Skip to content
Justice Tech School

Scope and Recursion

Variables are global unless declared local. Functions can call themselves.

Example
count=0
bump() { count=$((count + 1)); }
bump; bump
echo $count
fact() { local n=$1; if ((n <= 1)); then echo 1; else echo $((n * $(fact $((n - 1))))); fi; }
echo "$(fact 6)"

Result

2
720

Exercise

Make a variable local.

local =1

Quiz Test

Are Bash variables local by default?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.