MicroPosts
- Bash script prelude
#!/usr/bin/env bash set -euo pipefail IFS=$'\n\t' set -vx
Explanation:
#!/usr/bin/env bash
: interpret the subsequent lines withbash
set -e
: exit immediately with errorScript:
echo 1 not-existing-command echo 2
Out:
1 not-existing-command: command not found 2
Script:
set -e echo 1 not-existing-command echo 2
Out:
1 not-existing-command: command not found
set -u
: exit immediately with unbound variableScript:
echo 1 echo $NOT_EXISTING_VARIABLE echo 2
Out:
1 2
Script:
set -u echo 1 echo $NOT_EXISTING_VARIABLE echo 2
Out:
1 NOT_EXISTING_VARIABLE: unbound variable
set -o pipefail
: make errors fall through pipelinesScript:
not-existing-command | sort echo $?
Out:
not-existing-command: command not found 0
Script:
set -o pipefail not-existing-command | sort echo $?
Out:
not-existing-command: command not found 127
IFS=$'\n\t'
: Set input field separator (default: $' \n\t')Script:
string="1 2 3" for i in $string; do echo "$i" done IFS=$'\n\t' for i in $string; do echo "$i" done
Out:
1 2 3 1 2 3
Script:
array=( "a b" "c d" ) for x in ${array[@]}; do echo "$x" done IFS=$'\n\t' for x in ${array[@]}; do echo "$x" done
Out:
a b c d a b c d
set -x
: print lines as they are executedScript:
set -x echo 0 echo 1
Out:
+ echo 0 0 + echo 1 1