Reference · 35 entries
Bash and Linux Commands Reference
A Bash cheat sheet: builtins, conditions, loops, functions, arrays, expansions, redirection, strict mode, and the Linux text tools grep, sed, awk, sort, find and more, with real examples.
Bash 5 Version history
Every example is a Bash script that was run on Linux (Bash 5, GNU coreutils) when this page was built, so the output is real. For step-by-step explanations, see the Bash tutorial and the Linux and Bash for Developers course.
Shell Builtins and Syntax
Bash builtins and syntax: printf, read, conditions, loops, functions, arrays, expansions, redirection, strict mode and traps.
| Name | Description |
|---|---|
printf | Print formatted text with format specifiers like %s, %d and %.2f; more portable and predictable than echo. |
read | Read a line from standard input into variables, splitting on IFS; use -r to keep backslashes and -p for a prompt. |
mapfile | Read all lines of input into an indexed array at once (also called readarray), optionally trimming newlines with -t. |
[[ ]] | Bash's conditional expression: compare strings and numbers, test files, and match glob patterns or regular expressions. |
if | Run commands depending on whether a command succeeds (exit status 0), with optional elif and else branches. |
case | Match a value against glob patterns and run the first matching branch; cleaner than long if/elif chains. |
for | Loop over words, files, ranges or array elements, or use the C-style form for counters. |
while | Repeat commands while a condition succeeds; with read, it's the standard way to process input line by line. |
function | Group commands into a named function that takes arguments like a script ($1, $@) and returns an exit status. |
local | Declare variables that exist only inside the current function, so they don't overwrite globals with the same name. |
declare | Declare variables with attributes: arrays, associative arrays, integers, read-only values and exported variables. |
arrays | Indexed and associative arrays: create, append, read elements, count, slice and loop over them safely. |
${...} expansion | Defaults, length, substrings, prefix and suffix removal, search and replace, and case conversion on variables. |
$(( )) arithmetic | Evaluate integer arithmetic and comparisons with $(( )) and (( )); Bash has no floating point, so use awk or bc. |
redirection | Send output to files, read input from files, merge stderr into stdout, and discard output with /dev/null. |
here-document | Feed multiple lines to a command with <<EOF; quote the marker to stop variable expansion, use <<- to strip tabs. |
set -euo pipefail | Turn on strict mode: exit on errors (-e), fail on unset variables (-u), and fail pipelines when any part fails. |
trap | Run commands when the script exits or receives a signal; the standard way to clean up temporary files and child processes. |
export | Mark variables so child processes inherit them; one-off variables can also be set for a single command. |
source | Run a file's commands in the current shell, so its variables and functions stay defined; . is the POSIX spelling. |
getopts | Parse short command-line options such as -v and -o FILE in scripts, with errors for unknown options. |
Text and System Tools
Linux tools used with Bash: grep, sed, awk, cut, sort, uniq, tr, find, xargs, head, tail, wc, chmod, tar, ps and kill.
| Name | Description |
|---|---|
grep | Search files or input for lines matching a pattern, with options for case, counts, context, recursion and regex. |
sed | Edit text streams: substitute with s/old/new/, delete or print lines by number or pattern, and edit files in place. |
awk | Process text by fields: print and filter columns, compute sums and group totals, with a small built-in language. |
cut | Extract columns from each line by delimiter and field number, or by character positions. |
sort | Sort lines alphabetically, numerically, in reverse, by a key column, or with human-readable sizes; -u removes duplicates. |
uniq | Remove or count adjacent duplicate lines; sort first, then use -c to count occurrences or -d to show only duplicates. |
tr | Translate, squeeze or delete characters, such as changing case, removing carriage returns or splitting words onto lines. |
find | Find files and directories by name, type, size, age or permissions, and act on them with -exec, -delete or xargs. |
xargs | Build and run commands from input lines, in batches or in parallel; pair with find -print0 and xargs -0 for safe file names. |
head and tail | Show the first or last lines of files, skip headers, and follow growing log files with tail -f. |
wc | Count lines, words, characters and bytes in files or input; wc -l is the usual way to count lines or results. |
chmod | Change file permissions with symbolic modes (u+x, go-w) or octal numbers (755, 644, 600), optionally recursively. |
tar | Create, list and extract archives, usually gzip-compressed .tar.gz files, preserving directory structure and permissions. |
ps and kill | List processes with ps and send signals with kill: SIGTERM to ask a process to stop, SIGKILL only as a last resort. |