Jacob Paris
← Back to all content

Bash Cheatsheet

Run a then b

a; b

Run a , and if it succeeds run b

a && b

Run a if a directory exists

[[ -d node_modules ]] && a

Run npm i if a node_modules doesn't exist

[[ ! -d node_modules ]] && npm i

Run npm start in a directory src

(cd src; npm start)

Output text

echo "Text"

Output text to a file

echo "Text" > file.txt

Append text to a file

echo "Text" >> file.txt

Run a function

hello () {
echo "Hello!"
}
hello

Run a function with arguments

hello () {
echo "Hello, $1"
}
hello "world"

Print Variable VAR not set if VAR doesn't exist

: ${VAR:?"Variable VAR not set"}

Exit if docker is not installed

if [ ! "$(command -v docker)" ]; then
echo "🐳 Docker daemon not installed"
exit 1
fi

Suppress error output

docker run server 2>/dev/null
Professional headshot

Hi, I'm Jacob

Hey there! I'm a developer, designer, and digital nomad with a background in lean manufacturing.

About once per month, I send an email with new guides, new blog posts, and sneak peeks of what's coming next.

Everyone who subscribes gets access to the source code for this website and every example project for all my tutorials.

Stay up to date with everything I'm working on by entering your email below.

Unsubscribe at any time.