There are two typical ways of declaring a function. I prefer the second approach.
function function_name { command... }
or
function_name () { command... }
To call a function with arguments:
function_name "$arg1" "$arg2"
The function refers to passed arguments by their position (not by name), that is $1
, $2
, and so forth. $0
is the name of the script itself.
Example:
function_name () { echo "Parameter #1 is $1" }
Also, you need to call your function after it is declared.
#!/usr/bin/env sh foo 1 # this will fail because foo has not been declared yet. foo() { echo "Parameter #1 is $1" } foo 2 # this will work.
Output:
./myScript.sh: line 2: foo: command not found Parameter #1 is 2
Reference: Advanced Bash-Scripting Guide.
Related Posts:
- “Parameter” vs “Argument”
- How do I parse command line arguments in Bash?
- Return value in a Bash function
- find: missing argument to -exec
- Difference between return and exit in Bash functions
- Meaning of “[: too many arguments” error from if [] (square brackets)
- How do I pause my shell script for a second before continuing?
- Cp: target is not a directory
- How to create a file in Linux from terminal window? [closed]
- In the shell, what does ” 2>&1 ” mean?
- What is the preferred Bash shebang?
- How to compare strings in Bash
- How to decode Seagate’s hard drive date code in a Bash script
- What does export PS1=”\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]\$ ” mean in MacOS’ bash Terminal?
- How do I use a regex in a shell script?
- How do I execute a bash script in Terminal?
- How to make “if not true condition”?
- Multi-line string with extra space (preserved indentation)
- Bash script and /bin/bash^M: bad interpreter: No such file or directory
- An “and” operator for an “if” statement in Bash
- ‘\r’: command not found – .bashrc / .bash_profile [duplicate]
- Echo newline in Bash prints literal \n
- What is $@ in Bash? [duplicate]
- Echo newline in Bash prints literal \n
- What is $@ in Bash?
- How can I use “:” as an AWK field separator?
- An “and” operator for an “if” statement in Bash
- “while :” vs. “while true” [duplicate]
- How can I declare and use Boolean variables in a shell script?
- What is the meaning of 2 in 2> /dev/null
- How to reload .bash_profile from the command line?
- What is the purpose of “&&” in a shell command?
- Parsing JSON with Unix tools
- Bash scripting missing ‘]’
- Cannot use mkdir in home directory: permission denied (Linux Lubuntu)
- Using find command in bash script
- How can I count all the lines of code in a directory recursively?
- How can I kill a process by name instead of PID, on Linux?
- How to change the output color of echo in Linux
- Which characters need to be escaped when using Bash?
- How do you pass a function as a parameter in C?
- Delete all local git branches
- How can I get the source directory of a Bash script from within the script itself?
- How do you pass a function as a parameter in C?
- cp: missing destination file operand after
- BASH Syntax error near unexpected token ‘done’
- Shell: How to call one shell script from another shell script?
- bash assign default value
- What does -z mean in Bash?
- How can I match a string with a regex in Bash?
- echo that outputs to stderr
- How to define hash tables in Bash?
- How to escape single quotes within single quoted strings
- error: “initializer expression list treated as compound expression”
- What is cp: cannot stat error in Unix, I get this error when trying to copy thing from one folder to another
- Simple logical operators in Bash
- Bash if statement with multiple conditions throws an error
- Laravel PHP Command Not Found
- Pass a JavaScript function as parameter
- Getting an “ambiguous redirect” error
- Bash syntax error: unexpected end of file
- Create timestamp variable in bash script
- How to convert a string to lower case in Bash?
- Binary Data Posting with curl
- How to modify a global variable within a function in bash?
- How do I automatically restart a Minecraft Spigot server in the event of a crash or /stop when using screen?
- Simple average function in Javascript
- How do I remove the file suffix and path portion from a path string in Bash?
- Sorting data based on second column of a file
- Why can’t we pass arrays to function by value?
- Using SED with wildcard
- How do I add a newline using printf?
- zip error – Nothing to do
- How to remove last n characters from a string in Bash?
- Optional arguments in C function
- How to split one string into multiple strings separated by at least one space in bash shell?
- Create a function with optional call variables
- “Error installing rails” because “extconf.rb failed” on Ubuntu 18.04
- Make a Bash alias that takes a parameter?
- Copy multiple files from one directory to another from Linux shell
- How to ssh from within a bash script?
- curl: no URL specified for restful api
- How can I do a recursive find/replace of a string with awk or sed?
- Running bash script from within python
- How to run C program on Mac OS X using Terminal?
- bash : cd : too many arguments
- Bash mkdir and subfolders
- Javascript- Multiplying 2 numbers and return number
- How to open Emacs inside Bash
- Integer expression expected error in shell script
- Error when using ‘sed’ with ‘find’ command on OS X: “invalid command code .”
- How to determine if a bash variable is empty?
- What is the difference between double and single square brackets in bash?
- How do I get the current Unix time in milliseconds in Bash?
- How to create a UUID in bash?
- bash: print stderr in red color
- Clean way to write complex multi-line string to a variable
- How to make bash scripts print out every command before it executes?
- What does passing the -xe parameters to /bin/bash do
- Standard place for user defined bash_completion.d scripts?