Bash Cheatsheet
Run a then b
sh
a; bRun a , and if it succeeds run b
sh
a && bRun a if a directory exists
sh
[[ -d node_modules ]] && aRun npm i if a node_modules doesn't exist
sh
[[ ! -d node_modules ]] && npm iRun 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.txtAppend text to a file
sh
echo "Text" >> file.txtRun a function
sh
hello () { echo "Hello!"}helloRun 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 1fiSuppress error output
sh
docker run server 2>/dev/null