Tools › Cheat sheets › Linux & Shell › Bash
🐚

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

CommandWhat it does
Ctrl-rSearch your command history; press again for older matches.
Ctrl-a / Ctrl-eJump to the start / end of the line.
Ctrl-wDelete the word before the cursor.
Ctrl-u / Ctrl-kDelete to the start / end of the line.
Ctrl-lClear the screen.
Ctrl-cInterrupt the running command.
Ctrl-zSuspend the running command (resume with fg or bg).
Ctrl-dEnd 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 textSearch command history.

Redirection and pipes

CommandWhat it does
command > fileWrite output to a file, overwriting it.
command >> fileAppend output to a file.
command 2> errors.txtRedirect error output.
command > out.txt 2>&1Send output and errors to the same file.
command &> out.txtShorthand for the line above (Bash).
command 2>/dev/nullDiscard error messages.
command1 | command2Pipe one command's output into another.
command | tee fileShow output and save it to a file.
command | tee -a fileShow output and append it to a file.
command < fileRead input from a file.
cat <<'EOF' > file line one line two EOFWrite several lines to a file with a here-document.

Variables and substitution

CommandWhat it does
NAME=valueSet a variable (no spaces around =).
export NAME=valueSet 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

CommandWhat it does
#!/usr/bin/env bash set -euo pipefailScript header: stop on errors, unset variables and failed pipes.
if [ -f "$file" ]; then echo exists; fiTest whether a file exists.
if [[ "$a" == "$b" ]]; then echo same; fiCompare strings with [[ ]].
for f in *.txt; do echo "$f"; doneLoop over files.
while read -r line; do echo "$line"; done < fileRead a file line by line.
trap 'rm -f "$tmp"' EXITRun cleanup when the script exits.
[ "$#" -ge 1 ] || { echo "usage: $0 arg"; exit 1; }Require at least one argument.
command1 && command2Run command2 only if command1 succeeds.
command1 || command2Run command2 only if command1 fails.

Jobs

CommandWhat it does
command &Run a command in the background.
jobsList background jobs.
fg %1Bring job 1 to the foreground.
bg %1Resume a suspended job in the background.
nohup command &Keep running after you log out.
disown %1Detach 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.

More Linux & Shell cheat sheets

🔎 grep🔍 find✂️ sed🧮 awk📦 tar📝 Vim🪟 tmux🔐 chmod permissionsAll Linux & Shell cheat sheets →
Copied to clipboard