Shell scripting notes

Created at 2024-10-16 19:26
Last edited at 2024-10-17 21:39

POSIX

Redirect multiple lines at once

{
    echo hello
    echo world
    echo !
} > myfile

Useful options

set -o errexit # exit when a command returns a non-zero exit code
set -o nounset # exit when an undefined varialbe is used
set -o xtrace # print the currently executed line to stdout
set -o pipefail # (this one is bash only) stop a pipeline if a command returns a non-zero exit code

List all options

set -o

Bash

Add flags to a command conditionally

if [ "$myvar" = "value" ]; then
    params+=(--myflag 'my value')
fi
mycommand "${params[@]}"