Skip to content

Instantly share code, notes, and snippets.

@neilellis
Created April 16, 2015 15:05
Show Gist options
  • Save neilellis/22a725084fd839ee61f8 to your computer and use it in GitHub Desktop.
Save neilellis/22a725084fd839ee61f8 to your computer and use it in GitHub Desktop.
Some useful bash features
#!/usr/bin/env bash
set -eux
#e - error if non zero return value
#u - undefined variables trigger syntax error
#x - debug
#minus sign ADDS the flags
#plus sign REMOVES the flags
A=7
#Double parenthesis after $ performs arithmetic
B=$(($A * 4))
echo $B
#Double parenthesis in if statements allow arithemtic comparison
if (( $B > 25 ))
then
echo $B
echo ${C:-$((B+10))}
fi
set +u
#Double brackets allow proper variable usage not expansion
if [[ $1 == "thing" ]]
then
thing="Oops no args"
else
thing=$1
fi
set -u
#Default value
echo ${1:-"Oops no args"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment