Bash / Shell Commands Cheatsheet
Search and filter 85+ Bash and shell commands by category. Click any card to copy the command to your clipboard instantly.
Showing 85 of 85 commands — click any card to copy
Bash Variables & Scripts
Variables in Bash are assigned without spaces around the = sign: NAME="Alice". Access them with a leading $: echo $NAME. Always quote variables in double quotes to handle values with spaces: echo "$NAME".
Special variables worth knowing: $? holds the exit code of the last command (0 means success, non-zero means error). $$ is the current process ID. $# is the count of positional arguments. Use export to make a variable available to child processes.
Shell Redirection
Every process in Unix has three standard streams: stdin (file descriptor 0), stdout (fd 1), and stderr (fd 2). Redirection operators let you change where these streams flow:
>— redirect stdout to a file (overwrites)>>— append stdout to a file2>— redirect stderr to a file2>&1— redirect stderr to wherever stdout is pointing|— pipe stdout of one command to stdin of the next
The pipe operator | is one of the most powerful shell features — it lets you chain simple tools into powerful pipelines, e.g. cat access.log | grep "404" | sort | uniq -c | sort -rn | head -10.
Bash vs Zsh vs Fish
Bash is the most portable shell, present on virtually every Unix-like system and required for most shell scripts. It is the safe default for scripting. Zsh is a Bash-compatible superset with powerful interactive features — better tab completion, glob expansion, spelling correction, and the Oh My Zsh plugin ecosystem. It is the default on macOS since Catalina.
Fish (Friendly Interactive Shell) prioritises discoverability with inline auto-suggestions, syntax highlighting as you type, and a web-based configuration UI. However, Fish is not POSIX-compatible — its scripting syntax is different from Bash, so existing Bash scripts will not run in Fish without modification. Fish is excellent as an interactive shell but Bash or Zsh are better choices for portable scripting.
Frequently Asked Questions
What is the difference between single quotes and double quotes in Bash?
In Bash, single quotes preserve every character literally — no variable expansion, no command substitution, and no escape sequences occur inside single quotes. Double quotes allow variable expansion ($VAR), command substitution ($(cmd)), and some escape sequences (\n, \t), but suppress word splitting and glob expansion. Use single quotes when you want a truly literal string and double quotes when you need variable or command substitution while still protecting whitespace.
What does 2>&1 mean in shell redirection?
2>&1 redirects file descriptor 2 (stderr) to wherever file descriptor 1 (stdout) is currently pointing. So cmd > output.log 2>&1 sends both stdout and stderr to output.log. The order matters — you must redirect stdout first, then redirect stderr to stdout. The shorthand cmd &> output.log achieves the same result in Bash. This is essential for capturing all output (including errors) from a command.
What is the difference between $@ and $* in Bash?
Both $@ and $* expand to all positional parameters (arguments passed to the script or function). The difference appears when they are quoted: "$*" expands to a single word with all arguments joined by the first character of IFS (usually a space), while "$@" expands to separate quoted words for each argument. This means "$@" correctly handles arguments that contain spaces, making it the safe default for iterating or forwarding all arguments.
What is the difference between Bash, Zsh, and Fish?
Bash (Bourne Again Shell) is the default shell on most Linux distributions and macOS (before Catalina). It is the most portable and widely used. Zsh is compatible with Bash but adds features like better tab completion, spelling correction, theme support, and the popular Oh My Zsh framework — it became the default shell on macOS Catalina and later. Fish (Friendly Interactive Shell) prioritises user-friendliness with syntax highlighting, auto-suggestions, and web-based configuration, but is not POSIX-compatible, meaning many Bash scripts will not run in Fish without modification.
How do I make a Bash script executable and run it?
First, add a shebang line as the very first line of your script: #!/usr/bin/env bash. Then make it executable with chmod +x script.sh. You can now run it with ./script.sh (from the same directory) or with its full path. Alternatively, run it without the executable bit using bash script.sh, though this bypasses the shebang. To make the script available as a command from anywhere, move it to a directory on your PATH such as /usr/local/bin.