shBash

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 dateiPrint the entire content
head -n 5 datei tail -n 5 dateiBeginning / end
tail -f logFollow a growing file live
less dateiPage through (q quits, / searches)
wc -l dateiCount lines (-w words, -c bytes)
file datei stat dateiFile type / metadata

Pipes & Redirection

a | bstdout of a becomes stdin of b
cmd > dateistdout into a file (overwrites)
cmd >> dateiAppend stdout to a file
cmd < dateistdin from a file
cmd 2> fehler.logRedirect stderr only
cmd > alles.log 2>&1Redirect stdout and stderr together
cmd > /dev/null 2>&1Discard all output
cmd | tee dateiShow 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=wertMake available to child processes

Tests & Conditions

if [[ $a -gt 5 ]]; then ...; fiBranching 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 -geNumber comparisons in [[ ]]
(( a > b ))Arithmetic condition
case "$x" in a) ...;; b|c) ...;; *) ...;; esacPattern matching
cmd1 && cmd2 || cmd3On success / otherwise (caution: cmd3 also runs if cmd2 fails)

Loops & Functions

for x in a b c; do ...; doneOver a word list
for i in {1..5}; do ...; doneNumber range
for f in *.txt; do ...; doneOver files (glob)
while read -r zeile; do ...; done < dateiRead a file line by line
while (( i < 5 )); do ...; doneAs long as the condition holds
break continueEnd 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
shiftShift arguments to the left
getopts "vo:" optParse short options
$?Exit code of the last command (0 = success)
exit 1End the script with an error code
set -euo pipefailStrict mode: abort on errors, undefined variables, pipe failures
trap 'aufraeumen' EXITAlways 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.csvExtract fields with a delimiter
awk '{ s += $1 } END { print s }'Sum of a column
cut -d: -f1,3 sort -u uniq -cCut 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 +30Files older than 30 days
find . -size +10MFiles over 10 MB
find . -name "*.tmp" -deleteDelete matches (conditions first!)
find . -exec cmd {} +Apply a command to matches (bundled)
find . -print0 | xargs -0 cmdSafe with spaces in names
xargs -n 1 -P 4 cmdParallel: 1 argument each, 4 processes

Processes, cron & Permissions

cmd & jobs waitStart in background / jobs / wait
kill PID kill -9 PIDTerminate politely / force
ps aux | grep nameFind processes
chmod +x skript.shMake executable
chmod 640 datei chown user dateiPermissions (rw-r-----) / owner
crontab -e crontab -lEdit / show the cron table
*/15 * * * * befehlcron: every 15 minutes
nohup cmd > log 2>&1 &Keep running even if the terminal closes