Skip to content
Laviyx Learn

Try

Linux and Bash course menu

Bash Functions: Arguments, local and Return Values

Organise Bash scripts with functions: define and call them, pass arguments, use local variables, return values with echo and exit codes, and share code with source.

Intermediate8 min readUpdated
On this page

Functions let you name a block of commands and reuse it.

Define and call#

Terminal
greet() {
  echo "Hello!"
}
greet
greet
Output
Hello!
Hello!

A function must be defined before it is called.

Arguments#

Inside a function, $1, $2 and $# refer to the function's own arguments, and you call it like a command, with no brackets:

Terminal
greet() {
  echo "Hello, $1. You are from $2."
}
greet "Asha" "Hyderabad"
Output
Hello, Asha. You are from Hyderabad.

Local variables#

Variables are global by default, even inside a function. Use local to keep them there:

Terminal
total=100
change() {
  local total=5
  echo "Inside: $total"
}
change
echo "Outside: $total"
Output
Inside: 5
Outside: 100

Always use local for variables inside functions.

Returning values#

return sets only an exit code from 0 to 255, used for success or failure. To give back data, echo it, and capture the output with $(...):

Terminal
add() {
  echo $(( $1 + $2 ))
}
sum=$(add 7 5)
echo "Sum is $sum"
Output
Sum is 12

Use return to report success or failure, so that the function works in an if:

Terminal
is_even() {
  (( $1 % 2 == 0 ))
}
for n in 3 4; do
  if is_even "$n"; then
    echo "$n is even"
  else
    echo "$n is odd"
  fi
done
Output
3 is odd
4 is even

The last command's status becomes the function's return value.

Terminal
check_file() {
  if [[ ! -f "$1" ]]; then
    echo "Missing: $1" >&2
    return 1
  fi
  return 0
}
check_file /etc/hostname && echo "Found"
check_file /nothing || echo "Status was $?"
Output
Found
Missing: /nothing
Status was 1

>&2 sends a message to stderr, which keeps error text out of captured output.

All arguments#

"$@" means all the arguments, each kept as a separate word:

Terminal
show_all() {
  echo "Count: $#"
  for item in "$@"; do
    echo "- $item"
  done
}
show_all one "two words" three
Output
Count: 3
- one
- two words
- three

Share functions between scripts#

Put common functions in a file, and load it with source:

Terminal
cat > lib.sh <<'EOF'
log() {
  echo "[$1] $2"
}
EOF
cat > app.sh <<'EOF'
#!/bin/bash
source ./lib.sh
log INFO "Started"
log ERROR "Disk is full"
EOF
bash app.sh
Output
[INFO] Started
[ERROR] Disk is full

Structure of a script#

A good script has settings, then functions, then a main:

Terminal
cat > tidy.sh <<'EOF'
#!/bin/bash
LIMIT=2

report() {
  echo "Keeping the newest $LIMIT"
}

main() {
  report
  echo "Done"
}

main "$@"
EOF
bash tidy.sh
Output
Keeping the newest 2
Done

Common mistakes#

Practice#

Practice exercise

Write a function max that prints the larger of two numbers, and use it to print Max: 9 for 4 and 9.

Show solution
Terminal
max() {
  if (( $1 > $2 )); then
    echo "$1"
  else
    echo "$2"
  fi
}
echo "Max: $(max 4 9)"
Output
Max: 9

Key takeaway#

Next: Arguments and Exit Codes.

Finished this lesson?