Shell Scripting Gotchas and the Bad Part

Credit: wikipedia


File need to end with Blank line


Bash need space to evaluate. Thing that look usual in other language can be syntax error in bash.

ERROR command x not found
x = 5

The loop is separated by space too.

for i in "${arr[@]}"; do; echo $i; doneWork just like
for i in 1 2 3 "foo" "bar"; do; echo $i; done
for i in {1..100}; do; echo $i; done

Compare String vs Int

For number, Bash does not have but rather

For string, Bash use for comparing. For exist and not exist use


The ending of here document need to be first thing the line. In the example below, need to be at the beginning of the line without space or tab.

cat << FOO

The alternative operator to support indent needs tab NOT space.

The Bad Part

Bash function argument is meaningless

bash_function(a,b) {
echo "$a $b"

Something like this will fail. Bash take the argument through , ,…

So we need to assign it like this.

function bash_function {
local a=$1
local b=$2
... do other thing ...

To remind me of this fact, I prefer syntax of than the one with . One good part is Bash at least have function scope.

Bash function cannot return value

Although Bash has keyword, it doesn’t work as usual. It can only return exit status where mean success.

bash_function() {
... do something ...
return 0

You need to assign value to Global Variable OMG ! yes it is !