Skip to content

Instantly share code, notes, and snippets.

@mjambon
Created July 19, 2018 17:38
Show Gist options
  • Save mjambon/605f48e3f567e9f8af407e55a7336ed8 to your computer and use it in GitHub Desktop.
Save mjambon/605f48e3f567e9f8af407e55a7336ed8 to your computer and use it in GitHub Desktop.
Bash scoping. Does this program print 1, 2 or 3?
#! /bin/bash
x=1
f() {
echo "$x"
}
g() {
local x=2
f
}
x=3
g
@mjambon
Copy link
Author

mjambon commented Jul 19, 2018

It prints

2

@mjambon
Copy link
Author

mjambon commented Jul 19, 2018

Because bash functions execute in their caller's scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment