Jacob Paris
← Back to all content

Bash Cheatsheet

Run a then b

sh
a; b

Run a , and if it succeeds run b

sh
a && b

Run a if a directory exists

sh
[[ -d node_modules ]] && a

Run npm i if a node_modules doesn't exist

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

Run npm start in a directory src

sh
(cd src; npm start)

Output text

sh
echo "Text"

Output text to a file

sh
echo "Text" > file.txt

Append text to a file

sh
echo "Text" >> file.txt

Run a function

sh
hello () {
echo "Hello!"
}
hello

Run a function with arguments

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

Print Variable VAR not set if VAR doesn't exist

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

Exit if docker is not installed

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

Suppress error output

sh
docker run server 2>/dev/null
Professional headshot
Moulton
Moulton

Hey there! I'm a developer, designer, and digital nomad building cool things with Remix, and I'm also writing Moulton, the Remix Community Newsletter

About once per month, I send an email with:

  • New guides and tutorials
  • Upcoming talks, meetups, and events
  • Cool new libraries and packages
  • What's new in the latest versions of Remix

Stay up to date with everything in the Remix community by entering your email below.

Unsubscribe at any time.