Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

bash cheatsheet

📅 2020-Sep-17 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ bash ⬩ 📚 Archive

Here is some information I find useful when using bash as a shell or when writing bash scripts.

Use interactively

$ man bash
$ help set
$ echo $-

Use in script

echo "$FOOBAR"

The double quoting is needed to handle the case of the variable value having whitespace. But to have reliable scripts, it is better to always double quote all reads of variables.

FOOBAR=1
FOOBAR=$(ls -l)
FOOBAR=${ANOTHER_VAR}

Remember to not use any spaces before and after the =. Otherwise Bash will interpret the statement as a command.

local x  # x will be local variable
x=$(FOOBAR)
# Same as $foobar
${foobar}

# If variable is not set or null, use a different value
${foobar:-"some other value"}

# If variable is not set or null, print your error message and exit
${foobar:?"Error: foobar is not set"}

# Get length of value of variable
${#foobar}

# Remove prefix pattern from value in variable
${foobar#"tmp_"}

# Remove suffix pattern from value in variable
${foobar%".tmp"}

# Search and replace first instance of string with another string in variable
${foobar/"find this"/"replace with this"}

# Search and replace all instances of string with another string in variable
${foobar//"find this"/"replace with this"}

# Get a substring of value in variable
${foobar:offset:len}
exit 1
exit $RETURN_VALUE

Use return value 0 on success and any non-zero value (typically 1) to indicate failure.

cmp --quiet foobar.txt joe.txt
LS_FILES=$(ls -l)
echo "Something""Some other thing"
echo "$FOOBAR".tmp
echo .tmp"$FOOBAR"
echo "$FOOBAR1""$FOOBAR2"
FOOBAR="$FOOBAR".tmp
cmp --quiet foobar.txt joe.txt
CMP_EXIT_VALUE=$?
FOOBAR=100
test $FOOBAR -eq 9
echo $?

While this command can be used by itself, it is also important for writing if-else expressions. The test command has another equivalent form [...] that is more commonly used in if-else.

These are equivalent:

test $FOOBAR -eq 9
[ $FOOBAR -eq 9 ]

Note that it is important to put spaces after [ and before ] to avoid some Bash problems.

There is another [[ operator that is more complicated and is generally preferred to [. More info on that can be found here.

for FNAME in $(ls -1)
do
    echo "File:""$FNAME"
done

It is common practice to roll up the do to the previous line by using ;:

for FNAME in $(ls -1); do
    echo "File:""$FNAME"
done

A simple example:

FOOBAR=99
if [ $FOOBAR -eq 100 ]
then
    echo "FOOBAR is 100"
elif [ $FOOBAR -eq 99 ]
then
    echo "FOOBAR is 99"
else
    echo "FOOBAR is neither 100 nor 99"
fi

It is common practice to roll up the then to the previous line by using ;:

if [ $FOOBAR -eq 100 ]; then
    echo "FOOBAR is 100"
fi
if ! command -v foobar --blah --blah &> /dev/null
then
    echo "Program foobar does not exist"
fi

This can be used for example to compare file extension:

if [ ${FPATH: -3} != ".py" ]
then
    echo "It is a Py file"
fi
set -e
set -o pipefail
set -u
foobar() {
    echo $1  # Print first param
    return 1  # Return value of function
}

foobar  # Call without arguments
foobar "hello" "world"  # Call with two arguments

References