Bash scripting cheatsheet

By , last updated September 24, 2016

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.

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*