Skip to content

Instantly share code, notes, and snippets.

@egdoc
egdoc / bash-dynamic-scope.sh
Created February 8, 2019 11:16
Dynamic vs static/lexical scoping: bash vs python
#!/bin/bash
# Bash has "dynamic" scoping:
# Variable lookups occur in the scope where a function is CALLED
animal="dog"
function get_animal() {
echo "I have a ${animal}"
}
function mypet() {
@egdoc
egdoc / errtrap-inheritance-example-1.sh
Last active November 13, 2019 09:10
Inheritance of ERR traps
#!/bin/bash
# The -e or --errexit shell option causes the shell to exit immediately when
# a command returns a non-zero status (with some exceptions). An ERR trap gets
# executed before the shell exits
set -o errexit
trap 'echo trapped!' ERR
# This command will have a non-zero exit status, since a standard user has no
# permission to enter the /root directory. Because of the error the trap will