Krzysztof Kowalczyk Blog  •  Notes  •  My Software  •  My Documents

Bash programming basics

Script arguments

Script arguments are passed as $0 (name of the script), $1 (first argument) etc.

$* – all arguments, as single word.
$@ – all arguments, but each is seen as a separate word

Check if a given argument was provided

if [ ! -n "$1" ]
then
  echo "First argument not given"
fi

Handling errors from launched programs.

Exit code from last executed command is in $?.

if [ "$?" -ne "0" ]; then
  echo "last command failed"
  exit 1
fi
← newer • 58 of 661older →