A variable that a function sets is the script's variable, so a function can change a value the script still needs:
bash
count=3
check_oven() {
count=0
echo "oven checked"
}
echo "orders: $count"
check_oven
echo "orders: $count"text
orders: 3
oven checked
orders: 0 ← check_oven changed the script's countlocal count=0 inside the function makes a count that only the function sees. The script's count stays 3.
~/report.sh should end with shops: 4.
- Run
./report.sh. It counts the wrong number of shops, with no error. - Find the variable that
count_salesand the loop both use. - Make it
localinsidecount_sales, and run it again.
Objectives
./report.shwrites each shop's sales, thenshops: 4.count_saleskeeps its variable to itself, withlocal.