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.

NameDescription
printfPrint formatted text with format specifiers like %s, %d and %.2f; more portable and predictable than echo.
readRead a line from standard input into variables, splitting on IFS; use -r to keep backslashes and -p for a prompt.
mapfileRead 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.
ifRun commands depending on whether a command succeeds (exit status 0), with optional elif and else branches.
caseMatch a value against glob patterns and run the first matching branch; cleaner than long if/elif chains.
forLoop over words, files, ranges or array elements, or use the C-style form for counters.
whileRepeat commands while a condition succeeds; with read, it's the standard way to process input line by line.
functionGroup commands into a named function that takes arguments like a script ($1, $@) and returns an exit status.
localDeclare variables that exist only inside the current function, so they don't overwrite globals with the same name.
declareDeclare variables with attributes: arrays, associative arrays, integers, read-only values and exported variables.
arraysIndexed and associative arrays: create, append, read elements, count, slice and loop over them safely.
${...} expansionDefaults, length, substrings, prefix and suffix removal, search and replace, and case conversion on variables.
$(( )) arithmeticEvaluate integer arithmetic and comparisons with $(( )) and (( )); Bash has no floating point, so use awk or bc.
redirectionSend output to files, read input from files, merge stderr into stdout, and discard output with /dev/null.
here-documentFeed multiple lines to a command with <<EOF; quote the marker to stop variable expansion, use <<- to strip tabs.
set -euo pipefailTurn on strict mode: exit on errors (-e), fail on unset variables (-u), and fail pipelines when any part fails.
trapRun commands when the script exits or receives a signal; the standard way to clean up temporary files and child processes.
exportMark variables so child processes inherit them; one-off variables can also be set for a single command.
sourceRun a file's commands in the current shell, so its variables and functions stay defined; . is the POSIX spelling.
getoptsParse 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.

NameDescription
grepSearch files or input for lines matching a pattern, with options for case, counts, context, recursion and regex.
sedEdit text streams: substitute with s/old/new/, delete or print lines by number or pattern, and edit files in place.
awkProcess text by fields: print and filter columns, compute sums and group totals, with a small built-in language.
cutExtract columns from each line by delimiter and field number, or by character positions.
sortSort lines alphabetically, numerically, in reverse, by a key column, or with human-readable sizes; -u removes duplicates.
uniqRemove or count adjacent duplicate lines; sort first, then use -c to count occurrences or -d to show only duplicates.
trTranslate, squeeze or delete characters, such as changing case, removing carriage returns or splitting words onto lines.
findFind files and directories by name, type, size, age or permissions, and act on them with -exec, -delete or xargs.
xargsBuild and run commands from input lines, in batches or in parallel; pair with find -print0 and xargs -0 for safe file names.
head and tailShow the first or last lines of files, skip headers, and follow growing log files with tail -f.
wcCount lines, words, characters and bytes in files or input; wc -l is the usual way to count lines or results.
chmodChange file permissions with symbolic modes (u+x, go-w) or octal numbers (755, 644, 600), optionally recursively.
tarCreate, list and extract archives, usually gzip-compressed .tar.gz files, preserving directory structure and permissions.
ps and killList processes with ps and send signals with kill: SIGTERM to ask a process to stop, SIGKILL only as a last resort.

Was this page helpful?