Bash scripting cheatsheet

By , last updated June 28, 2019

Small snippets for simple bash scripting.

If / then / else

#!/bin/bash

var=$1

# -z checks if the variable is empty or not
if [[ -z $var ]]
then
    echo "empty"
else
    echo "not empty"
fi

Do a command per file in a listing (bash for loop)

# Echo all files in current folder
for files in *; do echo $files; done
new
test
test.sh

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.