14.04.2025

Bash Cheat Sheet

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

  1. Executing Commands and Programs
    • Function:
      • Launch programs, utilities, and scripts.
    • Commands/Constructs:
      • ls, grep, mkdir, cd, chmod, ./script.sh, bash.
  2. Automation and Scripting
    • Function:
      • Create and run scripts, conditions, loops.
    • Commands/Constructs:
      • if, for, while, function, case, exit, test.
  3. Variables and Data Handling
    • Function:
      • Create variables, arithmetic, string operations.
    • Commands/Constructs:
      • var=value, export, unset, ${var}, $((expression)).
  4. Process and Signal Management
    1. Function:
      1. Manage processes and signals.
    2. Commands/Constructs:
      1. & (background), kill, killall, jobs, fg, bg, Ctrl+C (SIGINT), Ctrl+Z (SIGTSTP).
  5. I/O Redirection and Pipes
    • Function:
      • Control data streams.
    • Commands/Constructs:
      • >, >>, 2>, &>, |.
  6. Filesystem Management and Globbing
    • Function:
      • Navigation, file operations, pattern matching.
    • Commands/Constructs:
      • cp, mv, rm, *, ?, [], {}.
  7. User Interaction and Convenience
    • Function:
      • History, autocompletion, aliases.
    • Commands/Constructs:
      • history, !!, !n, Tab, alias, man, --help.
  8. Advanced Expansion
    • Function:
      • Generate sequences and patterns.
    • Commands/Constructs:
      • {}, ~, $().

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:

name="User" # no spaces around '='
echo $name # output: User

Export variables to make them available to child processes (e.g., scripts):

export EDITOR=nano # set default editor

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:

sleep 30 & # run in 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:

pgrep firefox # find PID by name
ps aux | grep ssh # search via filter

I/O Redirection

Control data streams:

stdin (input), stdout (output), stderr (errors).

Key Operators:

Combined redirection:

Substitutions (Parameter Expansion)

String manipulation:

List generation:

echo file.{txt,log} # file.txt file.log

Arithmetic:

echo $((10 / 2)) # 5

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:

alias ll='ls -alh' # add to ~/.bashrc

History usage:

history|grep "git"# find all "git" commands

Quick correction:

^old^new # replace "old" with "new" in last command