Bash / Shell Cheat Sheet
Free Bash cheat sheet: navigation, files, pipes, variables, loops, conditionals and the shell-scripting patterns you actually use.
Navigation & Files
| Concept | Syntax | Example |
|---|---|---|
| Print working dir Shows the absolute path of the current directory. | pwd | pwd |
| List files -l long format, -a includes hidden dotfiles. | ls -la | ls -la |
| Change directory .. goes up; cd alone returns home. | cd path | cd /var/log
cd .. |
| Make / remove dir -p creates parent directories as needed. | mkdir / rmdir | mkdir -p a/b/c |
| Copy / move / delete -r for directories; rm -rf is irreversible — be careful. | cp / mv / rm | cp -r src dest
rm -rf old |
Viewing & Searching
| Concept | Syntax | Example |
|---|---|---|
| Show file contents less file paginates large files. | cat file | cat notes.txt |
| First / last lines -f follows a file as it grows. | head / tail | tail -f app.log |
| Search in files -r recursive, -i case-insensitive, -n adds line numbers. | grep pattern file | grep -ri "error" logs/ |
| Find files Searches by name, type, size, modified time and more. | find path -name pattern | find . -name "*.js" |
| Count lines/words -l lines, -w words, -c bytes. | wc -l file | wc -l data.csv |
Pipes & Redirection
| Concept | Syntax | Example |
|---|---|---|
| Pipe Feeds the output of one command into the next. | cmd1 | cmd2 | cat log | grep error | wc -l |
| Redirect output > overwrites; >> appends to the file. | cmd > file | ls > files.txt |
| Redirect input Feeds a file in as standard input. | cmd < file | sort < names.txt |
| Redirect errors 2>&1 merges stderr into stdout. | cmd 2> file | make 2> errors.log |
| Chain conditionally && runs next on success; || runs next on failure. | cmd1 && cmd2 / || | mkdir build && cd build |
Variables
| Concept | Syntax | Example |
|---|---|---|
| Assign No spaces around the = sign. | name=value | greeting="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=value | export 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
| Concept | Syntax | Example |
|---|---|---|
| If statement Mind the spaces inside [ ]; close with fi. | if [ cond ]; then ... fi | if [ -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 ... done | for f in *.txt; do
echo "$f"
done |
| While loop Runs as long as the condition is true. | while [ cond ]; do ... done | while [ $n -lt 5 ]; do
n=$((n + 1))
done |
| Case statement Pattern-matching alternative to many elifs. | case $x in ... esac | case $1 in
start) echo go;;
stop) echo halt;;
esac |
Scripting
| Concept | Syntax | Example |
|---|---|---|
| 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.sh | chmod +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 pipefail | set -euo pipefail |