Bash-gotchas

From I Will Fear No Evil
Jump to navigation Jump to search

Oddball things to remember when working in bash..

  • Exit codes are not always honored in function calls when it is inline
f() {
echo "test"; exit 2
}
f() {
echo "test"
exit 2
}
  • Breaking out of a function early can be a PITA
f() {
foo=yes
if [[ ${foo} = 'yes' ]]; then
  echo "blah"
  return 0
elif [[ $foo = 'no ]]; then
  echo "barf"
  return 1
else
  echo "baz"
fi
curl https//some.url
}
  • Eval can be useful to pull vars out of a function
danger() { danger="$*"; passback danger; }
eval `danger '; /bin/echo *'`
echo "$danger"
  • Lazy scripting can or will cause errs:
unset FOO
$ echo "$FOO"

$ echo "1$FOO1"
1
$ echo "1${FOO}1"
11
$ FOO=bar
$ echo "1${FOO}1"
1bar1
$ echo "1$FOO1"
1