Define and Call a Function
greet() {
echo "Hello, $1"
}
greet deployCall it by name, without parentheses, followed by its arguments. The other syntax uses the keyword function:
function greet {
echo "Hello, $1"
}Both work in Bash. The first form also works in POSIX sh, so it is the safer habit. In a one-line function the last command needs a semicolon before the closing brace: hi() { echo hi; }. If Bash itself is new to you, start with what is Bash.
Define Before Use
Bash reads a script from top to bottom, so a function must exist before the line that calls it runs. A common layout defines everything first and calls a main function on the last line:
#!/bin/bash
main() {
check_disk
}
check_disk() {
df -h /
}
main "$@"This works because main is called only after both functions are defined. Script basics are in how to write and run a Bash script.
Arguments
| Variable | Holds |
|---|---|
$1, $2 | First, second argument |
"$@" | All arguments, each kept whole |
$# | Number of arguments |
$0 | The script name |
backup() {
if [ "$#" -lt 1 ]; then
echo "usage: backup FILE..." >&2
return 1
fi
for f in "$@"; do
cp -- "$f" "$f.bak"
done
}
backup /etc/hosts /etc/fstabInside a function, $1 and the rest are the function's arguments, not the script's. Pass the script's arguments on with "$@".
Local Variables
Variables set in a function are global unless you declare them with local:
count=10
reset() {
local count=0
echo "inside: $count"
}
reset
echo "outside: $count" # still 10Make every helper variable local. It prevents a function from overwriting a variable the rest of the script relies on.
Return Status or Output
return sets an exit status from 0 to 255, like any command: 0 means success. Use it for yes-or-no answers. Without return, the status of the last command counts:
is_root() {
[ "$(id -u)" -eq 0 ]
}
if is_root; then
echo "Running as root"
fiTo hand back data, print it and capture it with command substitution:
disk_used() {
df -P / | awk 'NR==2 {print $5}' | tr -d '%'
}
used=$(disk_used)
echo "Root file system is ${used}% full"Do not try to return text or numbers above 255 with return: larger numbers wrap around, so 256 becomes 0. Right after a call, $? holds the status.
Functions as Better Aliases
An alias cannot use an argument in the middle of its command. A function in ~/.bashrc can:
mkcd() {
mkdir -p -- "$1" && cd -- "$1"
}
svclog() {
sudo journalctl -u "$1" -f
}Run source ~/.bashrc or open a new terminal to load them. More on the file in what is .bashrc.
Load a File of Functions with source
Keep shared helpers in one file, for example /usr/local/lib/helpers.sh:
log() {
printf '%s %s\n' "$(date '+%F %T')" "$*" >&2
}
die() {
log "ERROR: $*"
exit 1
}Load it at the top of each script:
#!/bin/bash
source /usr/local/lib/helpers.sh
log "Starting backup"
tar -czf /var/backups/etc.tar.gz /etc || die "tar failed"
log "Done"source, or its short form ., runs the file in the current shell, so its functions become available. Note that die uses exit, which ends the whole script, not only the function.
List and Remove Functions
declare -F: lists the names of all defined functions.type mkcd: prints the definition of one function.unset -f mkcd: removes it from the current shell.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations