Bash / Shell Cheat Sheet

Free Bash cheat sheet: navigation, files, pipes, variables, loops, conditionals and the shell-scripting patterns you actually use.

Navigation & Files

ConceptSyntaxExample
Print working dir
Shows the absolute path of the current directory.
pwdpwd
List files
-l long format, -a includes hidden dotfiles.
ls -lals -la
Change directory
.. goes up; cd alone returns home.
cd pathcd /var/log cd ..
Make / remove dir
-p creates parent directories as needed.
mkdir / rmdirmkdir -p a/b/c
Copy / move / delete
-r for directories; rm -rf is irreversible — be careful.
cp / mv / rmcp -r src dest rm -rf old

Viewing & Searching

ConceptSyntaxExample
Show file contents
less file paginates large files.
cat filecat notes.txt
First / last lines
-f follows a file as it grows.
head / tailtail -f app.log
Search in files
-r recursive, -i case-insensitive, -n adds line numbers.
grep pattern filegrep -ri "error" logs/
Find files
Searches by name, type, size, modified time and more.
find path -name patternfind . -name "*.js"
Count lines/words
-l lines, -w words, -c bytes.
wc -l filewc -l data.csv

Pipes & Redirection

ConceptSyntaxExample
Pipe
Feeds the output of one command into the next.
cmd1 | cmd2cat log | grep error | wc -l
Redirect output
> overwrites; >> appends to the file.
cmd > filels > files.txt
Redirect input
Feeds a file in as standard input.
cmd < filesort < names.txt
Redirect errors
2>&1 merges stderr into stdout.
cmd 2> filemake 2> errors.log
Chain conditionally
&& runs next on success; || runs next on failure.
cmd1 && cmd2 / ||mkdir build && cd build

Variables

ConceptSyntaxExample
Assign
No spaces around the = sign.
name=valuegreeting="hello"
Use a variable
${name} disambiguates when next to other text.
$name / ${name}echo "$greeting world"
Command substitution
Captures a command's output into a variable.
$(command)today=$(date +%F)
Environment variable
export makes a variable available to child processes.
export NAME=valueexport PATH="$PATH:/usr/local/bin"
Arguments
$@ is all args; $# is the count; $0 is the script name.
$1 $2 $@ $#echo "First arg: $1"

Conditionals & Loops

ConceptSyntaxExample
If statement
Mind the spaces inside [ ]; close with fi.
if [ cond ]; then ... fiif [ -f file.txt ]; then echo "exists" fi
Test operators
-f file, -d dir, -z empty string, -eq numeric equal.
-f / -d / -z / -eq[ -d /tmp ] [ "$x" -eq 5 ]
For loop
Iterates over words, globs, or a sequence.
for x in list; do ... donefor f in *.txt; do echo "$f" done
While loop
Runs as long as the condition is true.
while [ cond ]; do ... donewhile [ $n -lt 5 ]; do n=$((n + 1)) done
Case statement
Pattern-matching alternative to many elifs.
case $x in ... esaccase $1 in start) echo go;; stop) echo halt;; esac

Scripting

ConceptSyntaxExample
Shebang
The first line tells the OS which interpreter to use.
#!/bin/bash#!/bin/bash echo "Hello"
Make executable
Adds execute permission so you can run it directly.
chmod +x script.shchmod +x deploy.sh ./deploy.sh
Define a function
Call it like a command: greet Ada.
name() { ... }greet() { echo "Hi $1" }
Exit code
0 means success; $? holds the last command's exit code.
exit n / $?exit 1 echo $?
Safer scripts
Exit on error, unset variable, or failed pipe.
set -euo pipefailset -euo pipefail