Bash Cheat Sheet
Commands, scripting patterns, and shortcuts - searchable, filterable, copy-ready.
This Bash cheat sheet covers everything you need to work efficiently in the terminal, from basic navigation to advanced scripting patterns.
Bash is the default shell on Linux and most Unix-based systems. It's also a full scripting language. Knowing it well saves you hours.
What's Inside
-
Basics - navigation, environment variables, history, and help commands
-
Files & Directories - create, delete, copy, move, find, and inspect files
-
Text & Search - grep, sed, awk, xargs, pipes, and redirection
-
Processes - manage running processes, background jobs, and signals
-
Scripting - variables, arrays, loops, functions, conditionals, here-docs, trap, and cron
-
Network - curl, SSH, SCP, rsync, and diagnostics
-
Permissions - chmod, chown, and sudo patterns
-
Archive - tar, gzip, zip, and extraction commands
-
Shortcuts - keyboard shortcuts and useful one-liners
Who It's For
Developers, sysadmins, and DevOps engineers who work in the terminal daily. Also useful for anyone writing automation scripts or managing servers.
Every snippet is copy-ready. Most include a preview showing expected output or behavior. Compatibility badges mark which commands are POSIX-safe, GNU-specific, or require bash 4+.
Strict mode, signal handling, regex matching, associative arrays -- the parts that trip people up are all here, with working examples.
What is Bash
Bash is the Bourne-Again SHell, a Unix shell and command language that ships as the default login shell on most Linux distributions.
It works as both an interactive command-line interface and a scripting engine for automating tasks on Linux and Unix systems.
Developed by Brian Fox for the GNU Project in 1989, Bash replaced the original Bourne Shell (sh) while staying backward compatible with POSIX standards.
How Does Bash Differ from Other Shells
Bash supports arrays, arithmetic expansion, and command history out of the box - features sh lacks.
Zsh adds smarter tab completion and plugin ecosystems (Oh My Zsh).
Fish prioritizes user-friendliness with syntax highlighting and autosuggestions, but breaks POSIX compatibility.
Bash stays the safest choice for cross-platform shell scripting across Linux, macOS, and Windows Subsystem for Linux (WSL).
Bash Syntax
What is the Bash Shebang Line
#!/bin/bash goes on the very first line of any bash script.
It tells the OS to use the bash interpreter, not whatever shell the current user runs.
Without it, the script may execute under sh or another shell, breaking bash-specific syntax.
What Are Bash Comments
Single-line comments start with #.
Multi-line comments use the : '...' workaround - not a true comment block, but widely used in bash shell scripting.
How Do Bash Quoting Rules Work
-
Single quotes
'...'- literal string, no expansion -
Double quotes
"..."- allows$variableand command substitution -
Backticks
cmd- command substitution (prefer$(cmd)instead) -
Backslash
\- escapes single characters
Bash Variables
How Do You Declare and Use Variables in Bash
NAME="John"
echo "$NAME" # John
echo "${NAME}!" # John!
NAME = "John" # Error - no spaces around =
Always quote variables: "$NAME" prevents word splitting and glob expansion.
Variable Types in Bash
-
Local variables - exist only in the current shell session
-
Environment variables - exported with
export VAR=value, inherited by child processes -
Special variables -
$0(script name),$1–$9(positional args),$#(arg count),$@(all args),$?(last exit code),$$(current PID) -
Read-only variables - set with
readonly VAR=value
How Does String Manipulation Work in Bash
Bash handles string operations directly through parameter expansion - no external tools needed for basic tasks.
|
Operation |
Syntax |
Result |
|---|---|---|
|
Length |
|
character count |
|
Substring |
|
chars at offset 2, length 4 |
|
Replace first |
|
one substitution |
|
Replace all |
|
all matches |
|
Strip prefix |
|
removes shortest match |
|
Strip suffix |
|
removes shortest match |
|
Uppercase |
|
all caps |
|
Lowercase |
|
all lowercase |
What Are Bash Arrays
# Indexed array
fruits=("apple" "banana" "cherry")
echo "${fruits[0]}" # apple
echo "${fruits[@]}" # all elements
echo "${#fruits[@]}" # array length
# Associative array
declare -A colors
colors["red"]="#FF0000"
echo "${colors["red"]}"
Iterate with for item in "${fruits[@]}".
Bash Operators
What Are Bash Arithmetic Operators
Use $(( )) for integer math - no external tools required.
echo $((5 + 3)) # 8
echo $((10 % 3)) # 1
echo $((2 ** 8)) # 256
let and expr work too, but $(( )) is faster and cleaner.
What Are Bash Comparison Operators
[ ] is POSIX-compatible.
[[ ]] is bash-specific - safer, supports regex with =~, no need to quote variables.
Integer Comparison Operators Table
|
Operator |
Meaning |
|---|---|
|
|
equal to |
|
|
not equal to |
|
|
less than |
|
|
greater than |
|
|
less than or equal |
|
|
greater than or equal |
String Comparison Operators Table
|
Operator |
Meaning |
|---|---|
|
|
strings are equal |
|
|
strings are not equal |
|
|
string is empty |
|
|
string is not empty |
What Are Bash File Test Operators
File tests go inside [ ] or [[ ]].
|
Operator |
True when |
|---|---|
|
|
file exists and is a regular file |
|
|
directory exists |
|
|
readable |
|
|
writable |
|
|
executable |
|
|
file exists and size > 0 |
|
|
exists (any type) |
if [[ -f "/etc/hosts" ]]; then
echo "File exists"
fi
Bash Control Flow
How Do If Statements Work in Bash
if [[ "$name" == "John" ]]; then
echo "Hello John"
elif [[ "$name" == "Jane" ]]; then
echo "Hello Jane"
else
echo "Who are you?"
fi
Use && and || for inline conditionals: [[ -f file ]] && echo "exists".
How Do For Loops Work in Bash
# List iteration
for fruit in apple banana cherry; do
echo "$fruit"
done
# Range
for i in {1..5}; do echo "$i"; done
# C-style
for ((i=0; i<10; i++)); do echo "$i"; done
# Array
for item in "${arr[@]}"; do echo "$item"; done
How Do While and Until Loops Work in Bash
count=0
while [[ $count -lt 5 ]]; do
echo "$count"
((count++))
done
until runs while the condition is false - the inverse of while.
Use break to exit a loop early; continue to skip to the next iteration.
How Does a Case Statement Work in Bash
case "$1" in
start | up)
echo "Starting..."
;;
stop | down)
echo "Stopping..."
;;
*)
echo "Usage: $0 {start|stop}"
;;
esac
Cleaner than chained if/elif blocks when matching one variable against multiple patterns.
Bash Functions
How Do You Define and Call Functions in Bash
greet() {
echo "Hello, $1"
}
greet "World" # Hello, World
Both function greet { } and greet() { } syntax work.
Parameters use $1, $2, etc. - no argument declaration needed.
How Do You Return Values from Bash Functions
return only exits with a status code (0–255) - not a value.
To capture actual output, use command substitution:
get_name() { echo "John"; }
name=$(get_name)
echo "$name" # John
Check exit status with $? after any function call.
What is Variable Scope in Bash Functions
All variables are global by default - even ones set inside functions.
Use local to restrict scope:
myfunc() {
local x=10
echo "$x"
}
# $x is unavailable here
Bash Input and Output
How Do Bash Redirections Work
|
Syntax |
Action |
|---|---|
|
|
overwrite file with stdout |
|
|
append stdout to file |
|
|
read stdin from file |
|
|
redirect stderr |
|
|
merge stderr into stdout |
|
|
discard output |
What is Piping in Bash
| passes the stdout of one command as stdin to the next.
ls -la | grep ".sh" | wc -l
Pipes chain commands; redirections connect commands to files.
How Does Here Document (Heredoc) Work in Bash
cat <<EOF
Line one
Line two
EOF
Use <<-EOF to allow indented content.
Heredocs pass multi-line input to commands - useful for generating config files or SQL queries inside scripts.
What is Process Substitution in Bash
diff <(ls dir1) <(ls dir2)
<(command) treats command output as a temporary file.
Works where a filename is expected but you only have command output - can't be piped.
Bash File and Directory Commands
What Are the Core Bash File Management Commands
|
Command |
Purpose |
|---|---|
|
|
list all files with permissions |
|
|
copy directory recursively |
|
|
move or rename |
|
|
delete directory (no undo) |
|
|
create nested directories |
|
|
create empty file or update timestamp |
|
|
find files by name pattern |
|
|
make file executable |
How Do You Search File Contents in Bash
grep -rn "search_term" ./ # recursive, with line numbers
grep -i "pattern" file.txt # case-insensitive
grep -v "exclude" file.txt # invert match
sed handles find-and-replace in files; awk extracts and processes columns.
Bash Process Management
How Do You Manage Processes in Bash
ps aux # list all processes
kill 1234 # terminate PID 1234
kill -9 1234 # force kill
nohup ./script.sh & # run after logout
Find a PID with ps aux | grep process_name or pgrep process_name.
What Are Background and Foreground Jobs in Bash
./script.sh & # run in background
jobs # list background jobs
fg %1 # bring job 1 to foreground
bg %1 # resume job 1 in background
Ctrl+Z suspends the current foreground process; Ctrl+C terminates it.
Bash Scripting Patterns
How Do You Handle Script Arguments in Bash
echo "Script: $0"
echo "First arg: $1"
echo "All args: $@"
echo "Arg count: $#"
shift removes $1 and shifts remaining args down.
For flag parsing, use getopts:
while getopts "f:v" opt; do
case $opt in
f) file="$OPTARG" ;;
v) verbose=true ;;
esac
done
How Do You Handle Errors in Bash Scripts
set -e # exit on any error
set -u # error on undefined variables
set -o pipefail # catch errors in pipes
set -euo pipefail # all three together (common pattern)
Add a trap for cleanup on errors:
trap 'echo "Error on line $LINENO"' ERR
How Do You Debug a Bash Script
bash -n script.sh # syntax check only
bash -x script.sh # trace execution
set -x # enable trace inside script
set +x # disable trace
ShellCheck catches bugs statically before you run anything - use it.
What is the Difference Between [ ] and [[ ]] in Bash
[ ] is POSIX-portable but fragile - unquoted variables cause word splitting.
[[ ]] is bash-specific, supports regex matching with =~, handles empty variables safely, and is the right choice for any bash script.
[[ "$str" =~ ^[0-9]+$ ]] && echo "numeric"
Bash Keyboard Shortcuts
What Are the Most Useful Bash Keyboard Shortcuts
Learn these and you'll move through the terminal twice as fast.
Ctrl+R reverse-searches your bash command history.
Ctrl+L clears the screen without wiping history; Ctrl+C kills the current process; Ctrl+D closes the session.
Navigation Shortcuts
|
Shortcut |
Action |
|---|---|
|
|
move to start of line |
|
|
move to end of line |
|
|
move forward one word |
|
|
move backward one word |
Editing Shortcuts
|
Shortcut |
Action |
|---|---|
|
|
cut from cursor to line start |
|
|
cut from cursor to line end |
|
|
cut previous word |
|
|
paste (yank) cut text |
History Shortcuts
|
Shortcut |
Action |
|---|---|
|
|
reverse search history |
|
|
repeat last command |
|
|
last argument of previous command |
|
|
run command number |
Bash Configuration Files
What Are Bash Configuration Files
|
File |
Loaded when |
|---|---|
|
|
interactive non-login shell (new terminal tab) |
|
|
login shell (SSH, first login) |
|
|
on logout |
|
|
stores command history |
Most desktop terminals load ~/.bashrc.
SSH sessions load ~/.bash_profile - a common source of "my alias works locally but not on the server" confusion.
How Do You Create Bash Aliases
alias ll='ls -la'
alias gs='git status'
alias ..='cd ..'
Add aliases to ~/.bashrc for persistence.
unalias ll removes one; unalias -a removes all.
Bash String Operations
How Do You Perform String Operations in Bash
str="Hello World"
echo "${#str}" # 11 (length)
echo "${str^^}" # HELLO WORLD
echo "${str,,}" # hello world
echo "${str/World/Bash}" # Hello Bash
[[ "$str" == *"World"* ]] && echo "contains World"
Split a string by setting IFS:
IFS=',' read -ra parts <<< "a,b,c"
echo "${parts[0]}" # a
String Manipulation Reference
|
Operation |
Syntax |
Result |
|---|---|---|
|
Length |
|
character count |
|
Substring |
|
3 chars from offset 2 |
|
Replace first |
|
one substitution |
|
Replace all |
|
all matches |
|
Strip prefix |
|
removes shortest match |
|
Strip suffix |
|
removes shortest match |
Bash Conditional Expressions
What Are Bash Logical Operators
Inside [[ ]]: use && (AND), || (OR), ! (NOT).
[[ -f file && -r file ]] && echo "readable file"
[[ ! -d dir ]] && mkdir dir
Between commands: cmd1 && cmd2 runs cmd2 only if cmd1 succeeds; cmd1 || cmd2 runs cmd2 only if cmd1 fails.
How Does Brace Expansion Work in Bash
echo {a,b,c}.txt # a.txt b.txt c.txt
mkdir project/{src,tests,docs}
echo {1..5} # 1 2 3 4 5
echo {a..z} # a b c d ... z
cp config.yaml{,.bak} # quick backup trick
Brace expansion runs before any other expansion - no files need to exist.
Works inside for loops, mkdir, cp, mv, and anywhere a list of values is needed.
FAQ on Bash Cheat Sheets
What is a bash cheat sheet used for?
A bash cheat sheet is a quick reference for bash shell commands, syntax, and scripting patterns.
It saves time by keeping the most-used commands - file operations, loops, conditionals, redirections - in one scannable place instead of digging through man pages.
What are the most important bash commands to know?
ls, cd, cp, mv, rm, find, grep, chmod, and echo cover most daily Linux terminal work.
For bash scripting, add if, for, while, case, and function syntax to that list.
What is the difference between bash and shell scripting?
Shell scripting is the broad concept - writing scripts for any Unix shell.
Bash scripting is shell scripting specifically using the Bourne-Again SHell, which adds arrays, arithmetic expansion, and [[ ]] conditionals beyond basic POSIX sh.
How do I run a bash script?
Make it executable with chmod +x script.sh, then run it with ./script.sh.
Alternatively, call it directly with bash script.sh - no execute permission needed. Always start scripts with the #!/bin/bash shebang line.
What does $? mean in bash?
$? holds the exit code of the last executed command.
0 means success; any non-zero value means failure. Scripts commonly check $? after commands to decide whether to continue or trigger error handling.
What is the difference between single and double quotes in bash?
Single quotes preserve every character literally - no variable expansion, no special characters processed.
Double quotes allow $variable substitution and command substitution with $(), but still protect spaces and most special characters from the shell.
How do I debug a bash script?
Run bash -x script.sh to trace each command as it executes.
Add set -euo pipefail at the top of any script to catch errors, undefined variables, and failed pipes early. Tools like ShellCheck catch syntax issues before runtime.
What are bash special variables?
Special variables include $0 (script name), $1–$9 (positional arguments), $# (argument count), $@ (all arguments), $$ (current process ID), and $! (last background process ID).
These are read-only and set automatically by the shell.
How do I loop through files in bash?
Use a for loop with glob expansion:
for file in *.txt; do
echo "$file"
done
Always quote "$file" inside the loop to handle filenames with spaces correctly. Works across all Linux distributions without extra tools.
What is the safest way to write bash scripts?
Start every script with set -euo pipefail - exits on errors, catches undefined variables, and prevents silent pipe failures.
Use [[ ]] instead of [ ], quote all variables, and run scripts through ShellCheck before deploying to any production environment.