Bash cheatsheet – syntax overview
73 key commands and patterns for quick reference.
Navigation & Files Viewing Files Pipes & Redirection Variables & Expansion Tests & Conditions Loops & Functions Arguments & Exit Codes grep, sed, awk & Co. find & xargs Processes, cron & Permissions
Viewing Files
cat datei | Print the entire content |
head -n 5 datei tail -n 5 datei | Beginning / end |
tail -f log | Follow a growing file live |
less datei | Page through (q quits, / searches) |
wc -l datei | Count lines (-w words, -c bytes) |
file datei stat datei | File type / metadata |
Pipes & Redirection
a | b | stdout of a becomes stdin of b |
cmd > datei | stdout into a file (overwrites) |
cmd >> datei | Append stdout to a file |
cmd < datei | stdin from a file |
cmd 2> fehler.log | Redirect stderr only |
cmd > alles.log 2>&1 | Redirect stdout and stderr together |
cmd > /dev/null 2>&1 | Discard all output |
cmd | tee datei | Show output AND save it |
Variables & Expansion
x="wert" | Assignment (no spaces around =) |
echo "$x" echo "${x}text" | Read; braces delimit the name |
ergebnis=$(befehl) | Capture the output of a command |
$((a + b * 2)) | Integer arithmetic |
${x:-default} ${x:?Fehlertext} | Default value / abort if empty |
${f%.txt} ${f##*/} ${f/alt/neu} | Cut off suffix, cut off path prefix, replace |
export VAR=wert | Make available to child processes |
Tests & Conditions
if [[ $a -gt 5 ]]; then ...; fi | Branching with a number comparison |
[[ -f d ]] [[ -d d ]] [[ -e d ]] | File / folder / exists |
[[ -z "$s" ]] [[ -n "$s" ]] | Text empty / not empty |
[[ $s == a* ]] [[ $s =~ ^[0-9]+$ ]] | Pattern / regex |
-eq -ne -lt -le -gt -ge | Number comparisons in [[ ]] |
(( a > b )) | Arithmetic condition |
case "$x" in a) ...;; b|c) ...;; *) ...;; esac | Pattern matching |
cmd1 && cmd2 || cmd3 | On success / otherwise (caution: cmd3 also runs if cmd2 fails) |
Loops & Functions
for x in a b c; do ...; done | Over a word list |
for i in {1..5}; do ...; done | Number range |
for f in *.txt; do ...; done | Over files (glob) |
while read -r zeile; do ...; done < datei | Read a file line by line |
while (( i < 5 )); do ...; done | As long as the condition holds |
break continue | End the loop / next round |
f() { local x="$1"; echo "$x"; } | Function with a local parameter |
Arguments & Exit Codes
$0 $1 $2 $# "$@" | Script name, arguments, count, all individually |
shift | Shift arguments to the left |
getopts "vo:" opt | Parse short options |
$? | Exit code of the last command (0 = success) |
exit 1 | End the script with an error code |
set -euo pipefail | Strict mode: abort on errors, undefined variables, pipe failures |
trap 'aufraeumen' EXIT | Always run on exit |
grep, sed, awk & Co.
grep -rin "muster" ordner/ | Recursive, case-insensitive, with line numbers |
grep -v x grep -c x grep -E "a|b" | Invert / count / extended regex |
sed 's/alt/neu/g' | Replace (g = all matches per line) |
sed -n '5,10p' | Print only lines 5 to 10 |
awk -F, '{ print $1, $3 }' d.csv | Extract fields with a delimiter |
awk '{ s += $1 } END { print s }' | Sum of a column |
cut -d: -f1,3 sort -u uniq -c | Cut columns, sort+unique, count |
tr 'a-z' 'A-Z' tr -d '\r' | Translate / delete characters |
find & xargs
find . -name "*.log" | Search by name (quote patterns!) |
find . -type f -mtime +30 | Files older than 30 days |
find . -size +10M | Files over 10 MB |
find . -name "*.tmp" -delete | Delete matches (conditions first!) |
find . -exec cmd {} + | Apply a command to matches (bundled) |
find . -print0 | xargs -0 cmd | Safe with spaces in names |
xargs -n 1 -P 4 cmd | Parallel: 1 argument each, 4 processes |
Processes, cron & Permissions
cmd & jobs wait | Start in background / jobs / wait |
kill PID kill -9 PID | Terminate politely / force |
ps aux | grep name | Find processes |
chmod +x skript.sh | Make executable |
chmod 640 datei chown user datei | Permissions (rw-r-----) / owner |
crontab -e crontab -l | Edit / show the cron table |
*/15 * * * * befehl | cron: every 15 minutes |
nohup cmd > log 2>&1 & | Keep running even if the terminal closes |