Skip to content

Instantly share code, notes, and snippets.

@progrium
Last active Aug 29, 2015
Embed
What would you like to do?
#!/bin/bash
foo() {
echo "foo: exported from file1"
}
locals() {
bar() {
echo "bar: local to file1"
}
baz() {
echo "baz: local to file1"
}
}
main() {
locals
foo
bar
baz
}
[[ "$0" == "$BASH_SOURCE" ]] && main
# Output:
#
# foo: exported from file1
# bar: local to file1
# baz: local to file1
#!/bin/bash
source ./file1.sh
locals() {
bar() {
echo "bar: local to file2"
}
}
main() {
locals
foo
bar
type -t baz || echo "No baz loaded"
}
[[ "$0" == "$BASH_SOURCE" ]] && main
# Output:
#
# foo: exported from file1
# bar: local to file2
# No baz loaded
#!/bin/bash
source ./file1.sh; locals
foo() {
echo "foo: overridden in file3"
}
locals() {
baz() {
echo "baz: local to file3"
}
}
main() {
locals
foo
bar
baz
}
[[ "$0" == "$BASH_SOURCE" ]] && main
# Output:
#
# foo: overridden in file3
# bar: local to file1
# baz: local to file3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment