🐚
Bash cheat sheet
Bash is the default shell on most Linux systems. This sheet covers the keyboard shortcuts, redirection and scripting patterns you use every day.
Click any command to copy it.
Keyboard shortcuts
| Command | What it does |
|---|---|
Ctrl-r | Search your command history; press again for older matches. |
Ctrl-a / Ctrl-e | Jump to the start / end of the line. |
Ctrl-w | Delete the word before the cursor. |
Ctrl-u / Ctrl-k | Delete to the start / end of the line. |
Ctrl-l | Clear the screen. |
Ctrl-c | Interrupt the running command. |
Ctrl-z | Suspend the running command (resume with fg or bg). |
Ctrl-d | End of input; exits the shell on an empty line. |
!! | Repeat the previous command. |
sudo !! | Repeat the previous command with sudo. |
!$ | The last argument of the previous command. |
history | grep text | Search command history. |
Redirection and pipes
| Command | What it does |
|---|---|
command > file | Write output to a file, overwriting it. |
command >> file | Append output to a file. |
command 2> errors.txt | Redirect error output. |
command > out.txt 2>&1 | Send output and errors to the same file. |
command &> out.txt | Shorthand for the line above (Bash). |
command 2>/dev/null | Discard error messages. |
command1 | command2 | Pipe one command's output into another. |
command | tee file | Show output and save it to a file. |
command | tee -a file | Show output and append it to a file. |
command < file | Read input from a file. |
cat <<'EOF' > file
line one
line two
EOF | Write several lines to a file with a here-document. |
Variables and substitution
| Command | What it does |
|---|---|
NAME=value | Set a variable (no spaces around =). |
export NAME=value | Set a variable and pass it to child processes. |
echo "$NAME" | Use a variable (quote it to be safe). |
today=$(date +%F) | Store a command's output in a variable. |
echo "${NAME:-default}" | Use a default if the variable is unset or empty. |
echo $? | Exit status of the last command (0 means success). |
echo ${#NAME} | Length of a variable's value. |
echo ${NAME%.txt} | Remove a suffix from a value. |
Scripting
| Command | What it does |
|---|---|
#!/usr/bin/env bash
set -euo pipefail | Script header: stop on errors, unset variables and failed pipes. |
if [ -f "$file" ]; then echo exists; fi | Test whether a file exists. |
if [[ "$a" == "$b" ]]; then echo same; fi | Compare strings with [[ ]]. |
for f in *.txt; do echo "$f"; done | Loop over files. |
while read -r line; do echo "$line"; done < file | Read a file line by line. |
trap 'rm -f "$tmp"' EXIT | Run cleanup when the script exits. |
[ "$#" -ge 1 ] || { echo "usage: $0 arg"; exit 1; } | Require at least one argument. |
command1 && command2 | Run command2 only if command1 succeeds. |
command1 || command2 | Run command2 only if command1 fails. |
Jobs
| Command | What it does |
|---|---|
command & | Run a command in the background. |
jobs | List background jobs. |
fg %1 | Bring job 1 to the foreground. |
bg %1 | Resume a suspended job in the background. |
nohup command & | Keep running after you log out. |
disown %1 | Detach a job from the shell so it survives logout. |
Frequently asked questions
Why do I need quotes around variables?
Without quotes, values containing spaces or wildcards are split and expanded, which causes bugs. Write "$var" unless you specifically want splitting.
What does set -euo pipefail do?
-e exits on errors, -u treats unset variables as errors and -o pipefail makes a pipeline fail if any command in it fails.