Scoping in BASH

Callees inherit local variables

#! /usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail
shopt -s nullglob

print() {
  echo $(basename $0):${BASH_LINENO[0]}:${FUNCNAME[1]}"()" "$*"

other_method() {
  print "I can read cache_dir=${cache_dir}"

main() {
  local cache_dir=/var/cache
  print "I have set cache_dir=${cache_dir}"
  print "Now calling other_method()"

main "$@"
echo "$0 root here, now calling other_method"

Can you guess the output? I was very surprised the first time I saw this:

$ ./ I have set cache_dir=/var/cache Now calling other_method() I can read cache_dir=/var/cache root here, now calling other_method line 16: cache_dir: unbound variable

Licensed under CC BY Creative Commons License ~ ✉ torstein.k.johansen @ gmail ~ 🐘 ~ 🐦 @torsteinkrause