Function Arguments
In this page:
Function Arguments
Functions receive arguments the same way scripts do: $1, $2, etc. for individual arguments, $# for the count, and $@ for all of them. There's no separate parameter-declaration syntax like in other languages — arguments are simply read positionally inside the function body. Function-local $1 shadows the script's own $1 while the function is running.
Note: Inside a function, $1 refers to the function's own first argument, not the script's.
Example: Function Arguments
#!/bin/bash
greet() {
echo "Hello, $1! You are $2 years old."
}
greet "Alice" 30
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- Function Basics
- The read Command and Here-Strings
- Function Arguments
- stdin, stdout, stderr and File Descriptors
- Return Values
- Redirection Operators
- Local Variables
- Pipes and Chaining Commands
- Recursive Functions
- Here-Documents (<<EOF)
- Function Libraries
- The tee Command
- Discarding Output with /dev/null