The question "What is Bash?" was addressed in the article What is Linux Bash and How to Use It?. In this guide, we will delve deeper into Bash, exploring its features, capabilities, and key functionalities.
Bash (Bourne-Again SHell) is the default command shell for Unix-like systems, combining automation power, flexible process management, and ease of scripting. This cheat sheet covers key aspects of working with Bash: basic commands for navigation and file management, I/O redirection, variables, loops, conditions, and productivity tips (aliases, autocompletion, command history). The material is suitable for both beginners learning the terminal and experienced users seeking a quick reference for daily tasks — from server administration to routine automation.
Bash is an extremely versatile tool capable of performing numerous functions. Below is a table of essential Bash commands categorized by use case:
Bash Command Cheat Sheet by Category
- Executing Commands and Programs
- Function:
- Launch programs, utilities, and scripts.
- Commands/Constructs:
- ls, grep, mkdir, cd, chmod, ./script.sh, bash.
- Function:
- Automation and Scripting
- Function:
- Create and run scripts, conditions, loops.
- Commands/Constructs:
- if, for, while, function, case, exit, test.
- Function:
- Variables and Data Handling
- Function:
- Create variables, arithmetic, string operations.
- Commands/Constructs:
- var=value, export, unset, ${var}, $((expression)).
- Function:
- Process and Signal Management
- Function:
- Manage processes and signals.
- Commands/Constructs:
- & (background), kill, killall, jobs, fg, bg, Ctrl+C (SIGINT), Ctrl+Z (SIGTSTP).
- Function:
- I/O Redirection and Pipes
- Function:
- Control data streams.
- Commands/Constructs:
- >, >>, 2>, &>, |.
- Function:
- Filesystem Management and Globbing
- Function:
- Navigation, file operations, pattern matching.
- Commands/Constructs:
- cp, mv, rm, *, ?, [], {}.
- Function:
- User Interaction and Convenience
- Function:
- History, autocompletion, aliases.
- Commands/Constructs:
- history, !!, !n, Tab, alias, man, --help.
- Function:
- Advanced Expansion
- Function:
- Generate sequences and patterns.
- Commands/Constructs:
- {}, ~, $().
- Function:
Symbols/Operators: >, >>, |, &, *, ?, [], {}, ~ — not commands but widely used in Bash for data and process control.
Key Combinations: Ctrl+C, Ctrl+Z, Tab — shortcuts for process control and autocompletion.
Scripting Constructs: if, for, while, function — syntax elements for writing scripts.
For detailed scripting methods, refer to Linux Bash Basics. Scripting guide, and command explanations can be found in Ultimate Linux Commands Cheat Sheet. Below, we focus on Variables and Environment, Process Management, I/O Redirection, Command History, Aliases, Substitutions, and Hotkeys.
Variables and Environment
Local variables store data for the current session:
echo $name # output: User
Export variables to make them available to child processes (e.g., scripts):
Special Variables:
$? — exit code of the last command (0 = success);
$$ — PID of the current process;
$PATH — search paths for executables.
Substitutions for flexibility:
${var:-default} — returns default if var is unset;
${str// /_} — replaces all spaces with underscores.
Tip: Configure environment variables in ~/.bashrc to persist across sessions.
Process Management
Background mode: Append & to run a command in the background:
Pause/resume:
Ctrl+Z — pause a process;
fg — resume in foreground, bg — continue in background.
Terminate processes:
Ctrl+C — graceful termination (SIGINT);
kill -9 PID — force termination (SIGKILL).
Finding a process PID:
ps aux | grep ssh # search via filter
I/O Redirection
Control data streams:
stdin (input), stdout (output), stderr (errors).
Key Operators:
- > — overwrite a file:
-
ls > files.txt # save file list
- >> — append to a file;
2> — redirect errors to a file: -
grep "text" nofile.txt 2> errors.log
| — pipe output between commands:
-
cat logs.txt | grep "ERROR" | sort
Combined redirection:
-
script.sh &> output.log # combine stdout + stderr
Substitutions (Parameter Expansion)
String manipulation:
- Remove part of a string:
-
file="/home/user/doc.txt"
echo ${file##*/} # doc.txt (like `basename`)
-
- Replace text:
-
str="hello world"
echo ${str// /_} # hello_world
-
List generation:
Arithmetic:
Hotkeys
Command editing:
Ctrl+A / Ctrl+E — jump to start/end of line;
Ctrl+W / Alt+D — delete word left/right;
Ctrl+T — swap characters.
Command history:
Ctrl+R — search history;
!! — repeat last command;
!$ — use last argument of previous command.
Autocompletion:
Tab — autocomplete filename/command;
Double Tab — show all options.
Pro Tips
Aliases for frequent commands:
History usage:
Quick correction: