Bash-gotchas

From I Will Fear No Evil
Revision as of 15:40, 5 November 2021 by Chubbard (talk | contribs) (Created page with "Oddball things to remember when working in bash.. * Exit codes are not always honored in function calls when it is inline <pre> f() { echo "test"; exit 2 } f() { echo "test"...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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"