# Script skeletonbash Explain Copy
#!/usr/bin/env bash
# name.sh - one line saying what this does
set -Eeuo pipefail
shopt -s inherit_errexit nullglob
export PATH = / usr / local / sbin :/ usr / local / bin :/ usr / sbin :/ usr / bin :/ sbin :/ bin
export LC_ALL = C
SCRIPT_DIR =$( cd -- " $( dirname -- " ${ BASH_SOURCE [ 0 ]} " ) " && pwd -P )
readonly SCRIPT_DIR # assign first: readonly X=$(cmd) hides cmd's failure
log () { printf ' %(%Y-%m-%dT%H:%M:%S%z)T %-5s %s\n ' -1 " $1 " " ${ *: 2 } " >&2 ; }
die () { log ERROR " $1 " ; exit " ${2 :- 1} " ; }
need () { command -v " $1 " > /dev/null || die " missing command: $1 " 127 ; }
tmpdir = ""
cleanup () { [[ -n $tmpdir ]] && rm -rf -- " $tmpdir " ; }
trap cleanup EXIT
trap ' log ERROR "line $LINENO: $BASH_COMMAND exited $?" ' ERR
trap ' exit 130 ' INT
trap ' exit 143 ' TERM
main () {
need jq
log INFO " starting from $SCRIPT_DIR "
tmpdir =$( mktemp -d )
exec 9> /run/lock/name.lock
flock -n 9 || { log WARN " already running " ; exit 0 ; }
# ... work ...
}
main " $@ " Save it, chmod +x, and check it with shellcheck -x name.sh before the first real run.
# Running and debuggingTask Command Run with shebang (needs +x) ./script.shRun, ignoring shebang bash script.shRun in current shell (keeps cd, vars) source script.sh or . script.shSyntax check only bash -n script.shTrace every command bash -x script.shTrace a section set -x; ...; set +xBetter trace prefix export PS4='+ ${BASH_SOURCE##*/}:${LINENO}:${FUNCNAME[0]:-main}(): 'Trace to a separate file exec 7>>trace.log; BASH_XTRACEFD=7; set -xTrace without echoing set +x { set +x; } 2>/dev/nullVerbose (print lines as read) bash -v script.shRun with an empty environment (cron-like) env -i bash ./script.shWhat is this name? type -a cmd, command -v cmdLint shellcheck -x script.shLint for POSIX sh shellcheck -s sh script.shFormat check shfmt -d -i 2 -ci script.shCount syscalls / forks strace -c -f ./script.shTime a run time ./script.sh
# Variables and quotingForm Meaning name=valueAssign (no spaces around =) "$var"Expand as one word, no globbing. Default choice 'literal $var'Nothing expands $'tab\there\n'ANSI-C quoting: \t, \n, \x41, é "$(cmd)"Command substitution, trailing newlines stripped $((a + b * 2))Arithmetic expansion (integers only) export VAR=xPass to child processes VAR=x cmdSet only for that one command readonly VAR=x, declare -rConstant declare -i n=5Integer attribute declare -l s, declare -u sAuto lower / upper case on assignment declare -n ref=varName reference (Bash 4.3+) local vFunction-scoped (dynamic scope) unset var, unset -f fnRemove variable / function printf -v out '%05d' 42Format into a variable, no fork "$@"All args, each as its own word. Use this "$*"All args joined by first char of IFS into one word
# Special parameters and variablesName Value $0Script name as invoked $1 … ${10}Positional arguments (braces from 10) $#Number of positional arguments $?Exit status of the last command $$PID of the shell (the parent, even inside subshells) $BASHPIDPID of the current (sub)shell $!PID of the last background job $_Last argument of the previous command $-Current shell flags (i = interactive) ${PIPESTATUS[@]}Exit status of each stage of the last pipeline $LINENO, $FUNCNAME, ${BASH_SOURCE[0]}Current line, function stack, file $BASH_COMMANDCommand being executed (useful in traps) $BASH_REMATCHCaptures from the last [[ =~ ]] $BASH_VERSINFOVersion array, e.g. (( BASH_VERSINFO[0] >= 5 )) $RANDOM, $SRANDOM0–32767; 32-bit random (5.1+) $SECONDSSeconds since the shell started (assignable) $EPOCHSECONDS, $EPOCHREALTIMEUnix time, with microseconds (5.0+) $IFSField separators, default space, tab, newline $OPTIND, $OPTARGgetopts state$PWD, $OLDPWD, $HOME, $UID, $EUIDSelf-explanatory; $EUID is 0 for root
# Parameter expansionForm Result for p=/var/log/app.log.gz (or as noted) ${var:-def}def if unset or empty (${var-def}: only if unset)${var:=def}As above, and assign it ${var:?msg}Exit with msg if unset or empty ${var:+alt}alt if set and non-empty, else nothing${#p}Length: 20 ${p#*/}Remove shortest prefix match: var/log/app.log.gz ${p##*/}Remove longest prefix match (basename): app.log.gz ${p%/*}Remove shortest suffix match (dirname): /var/log ${p%%.*}Remove longest suffix match: /var/log/app ${p%.gz}Strip a known suffix: /var/log/app.log ${p##*.}Extension: gz ${p/log/LOG}Replace first: /var/LOG/app.log.gz ${p//log/LOG}Replace all: /var/LOG/app.LOG.gz ${p/#\/var/\/srv}Replace at start: /srv/log/app.log.gz ${p/%.gz/.zst}Replace at end: /var/log/app.log.zst ${p:5:3}Substring (offset 5, length 3): log ${p: -6}Last 6 chars (note the space): log.gz ${s^}, ${s^^}Upper-case first / all (4.0+) ${s,}, ${s,,}Lower-case first / all ${s@Q}Quoted for reuse as shell input (4.4+) ${!ref}Indirect: value of the variable named by ref ${!prefix@}Names of variables starting with prefix ${arr[@]+"${arr[@]}"}Empty-array-safe expansion under set -u on Bash < 4.4
Patterns in #, %, / are globs, not regex. shopt -s extglob adds ?(a|b), *(…), +(…), @(…), !(…).
# Arraysbash Explain Copy
# Indexed
arr =( one " two words " three )
arr += ( four ) # append
echo " ${ arr [ 0 ]} " " ${ arr [ -1 ]} " # first, last (negative index 4.3+)
echo " ${ # arr [ @ ]} " # length: 4
echo " ${ ! arr [ @ ]} " # indices: 0 1 2 3
echo " ${ arr [ @ ] : 1 : 2 } " # slice: two words three
for x in " ${ arr [ @ ]} " ; do printf ' [%s]\n ' " $x " ; done
unset ' arr[1] ' # remove element (indices are not renumbered)
mapfile -t lines < file.txt # file -> array, one line per element (readarray)
mapfile -t pods < <( kubectl get pods -o name )
IFS = , read -r -a fields <<< " a,b,c "
printf ' %s\n ' " ${ arr [ @ ]} " | sort # sort an array
# Associative (Bash 4+)
declare -A port =([ web ]= 80 [ db ]= 5432 )
port [ cache ]= 6379
echo " ${ port [ db ]} "
for k in " ${ ! port [ @ ]} " ; do echo " $k= ${ port [ $k ]} " ; done # order is not defined
[[ -v port [ web ] ]] && echo " has web " # key exists (4.3+)
unset ' port[cache] '
# Commands in arrays, never in strings
cmd =( rsync -a --delete --exclude ' node_modules ' )
[[ $dry == 1 ]] && cmd += ( --dry-run )
" ${ cmd [ @ ]} " src/ dst/ # Test operatorsFiles ([[ -f $f ]])
Op True if Op True if -eexists -sexists and size > 0 -fregular file -ddirectory -L / -hsymlink -pnamed pipe -Ssocket -b / -cblock / char device -r -w -xreadable / writable / executable by you -Oowned by you a -nt ba newer than b a -ot ba older than b a -ef bsame inode -t 0fd 0 is a terminal
Strings
Op True if -z $sempty -n $snot empty $a == $bequal (= also works) $a != $bnot equal $a < $b, $a > $bsorts before / after (inside [[ ]]) $s == *.logglob match (right side unquoted, [[ ]] only) $s =~ ^v[0-9]+$regex match (right side unquoted; captures in BASH_REMATCH) -v namevariable is set (4.2+)
Integers
[[ ]] / [ ](( ))Meaning -eq==equal -ne!=not equal -lt / -le< / <=less than / or equal -gt / -ge> / >=greater than / or equal
[ ] vs [[ ]] vs (( ))
[ ] / test[[ ]](( ))Type Command (POSIX) Bash keyword Bash arithmetic Word splitting of vars Yes: quote everything No No &&, || insideNo (use -a/-o, deprecated) Yes Yes Glob / regex match No Yes No Use for POSIX sh scripts Strings and files in Bash Numbers
Leading zeros make (( )) read numbers as octal: (( 10#$hour > 8 )). Decimals are not supported: compare with awk.
# Conditionals and loopsbash Explain Copy
if [[ -f $conf ]]; then ... ; elif [[ -d $conf ]]; then ... ; else ... ; fi
if grep -q ERROR " $log " ; then ... ; fi # test any command's exit status
[[ -n $x ]] && echo set || echo empty # not a real if/else: echo set failing runs both
case $1 in
start | up ) do_start ;;
stop ) do_stop ;;
* .tar.gz ) extract " $1 " ;;
-h | --help ) usage ; exit 0 ;;
*) usage >&2 ; exit 2 ;;
esac
for f in /var/log/app/*.log ; do ... ; done # glob (+ shopt -s nullglob)
for i in { 1..10} ; do ... ; done # brace range (literal numbers only)
for (( i = 0 ; i < n ; i ++ )); do ... ; done # C-style
while IFS = read -r line || [[ -n $line ]]; do ... ; done < file # line by line, last line safe
while IFS = : read -r user _ uid _ ; do ... ; done < /etc/passwd # split fields
while read -r -d '' f ; do ... ; done < <( find . -type f -print0 ) # NUL-safe
until curl -fsS " $url " > /dev/null ; do sleep 2 ; done
select opt in deploy rollback quit ; do ... ; break ; done # interactive menu
break 2 ; continue # leave / skip (nested levels) # Functionsbash Explain Copy
fn () { # or: function fn { ... }
local name = $1 count = ${2 :- 1} # local = function scope
local out
out =$( some_cmd ) || return 1 # separate declare and assign (SC2155)
printf ' %s\n ' " $out " # "return" data by printing it
return 0 # status 0-255
}
result =$( fn arg ) # capture output (runs in a subshell)
if fn arg > /dev/null ; then ... ; fi # use the status
declare -f fn # print the definition
export -f fn # make it available to bash -c children (xargs, parallel) Bash scope is dynamic : a function sees its caller's local variables. A function called in $(...) cannot change the caller's variables.
# Redirection, here-docs and process substitutionForm Meaning cmd > f / cmd >> fstdout to file (truncate / append) cmd 2> fstderr to file cmd > f 2>&1both to file (order matters) cmd &> f, cmd &>> fboth to file (Bash) cmd 2>&1 | tee -a logboth to screen and log cmd < fstdin from file cmd 2>/dev/nulldiscard stderr >&2 echo "msg"print to stderr exec >> log 2>&1redirect the rest of the script exec > >(tee -a log) 2>&1rest of the script to screen and log exec 9> file / exec 9>&-open / close fd 9 exec {fd}> filelet Bash pick a free fd number cmd <<< "$var"here-string as stdin diff <(cmd1) <(cmd2)process substitution as a file cmd > >(gzip > out.gz)output process substitution set -o noclobber, >|refuse to overwrite / force overwrite
bash Explain Copy
cat << EOF # unquoted delimiter: $vars and $(cmd) expand
host=$HOST date= $( date +%F )
EOF
cat << 'EOF' # quoted delimiter: nothing expands (templates, nginx $host)
proxy_set_header Host $host;
EOF
cat <<- EOF # <<- strips leading TABS (not spaces), for indented code
indented with tabs
EOF
sudo tee /etc/app.conf > /dev/null << 'EOF' # here-doc into a root-owned file
key=value
EOF # Exit codesCode Meaning 0Success 1General error (also grep no match, diff files differ) 2Misuse: bad option or syntax (grep/diff real error) 124timeout expired125timeout itself failed126Found but not executable (permission, noexec) 127Command not found 128+NKilled by signal N 130SIGINT (Ctrl+C) 137SIGKILL (kill -9, OOM killer, docker stop timeout) 141SIGPIPE (writer after reader closed: cmd | head) 143SIGTERM (kill, systemctl stop, docker stop) 255ssh connection error; exit -1 wraps to 2550/1/2/3Monitoring plugins: OK / WARNING / CRITICAL / UNKNOWN
bash Explain Copy
cmd ; rc = $? # save immediately
cmd || rc = $? # same, safe under set -e
if ! cmd ; then echo " failed " ; fi # $? here is 0 (the negation)
a | b | c ; echo " ${ PIPESTATUS [ @ ]} " # status of each stage
exit " $rc " # propagate # Strict mode: set -euo pipefailOption Effect set -e (errexit)Exit when an unhandled command fails set -u (nounset)Error on unset variables (${var:-} for optional ones) set -o pipefailPipeline fails if any stage fails set -E (errtrace)ERR trap is inherited by functions and subshellsshopt -s inherit_errexitCommand substitutions inherit -e (4.4+) set -x / set +xTrace on / off set -fDisable globbing set -Cnoclobber
Where set -e does not stop you
Situation Example Fix Condition of if, while, until if f; then (whole body of f ignores -e)Check steps inside f with || return 1 Left side of && / || f && echo okSame After ! ! cmdExplicit if local/export/readonly with substitutionlocal x=$(false)local x; x=$(false)Command substitution body x=$(false; echo hi)inherit_errexit, one command per $()Background jobs cmd & then waitwait "$pid" per jobArithmetic that evaluates to 0 kills you ((i++)) with i=0i=$((i + 1))Expected "no match" kills you n=$(grep -c x f)|| true on that commandPipeline SIGPIPE under pipefail cmd | head -1 returns 141Read all input, or accept 141
# trap and signalsbash Explain Copy
trap ' rm -rf -- "$tmp" ' EXIT # single quotes: expands when the trap runs
trap cleanup EXIT # or a function
trap ' on_err $LINENO ' ERR # with set -E
trap ' exit 130 ' INT ; trap ' exit 143 ' TERM # stop loops cleanly; EXIT trap still runs
trap '' HUP # ignore a signal
trap - INT # restore default
trap -p # list traps
kill -l # list signal names/numbers
kill -TERM " $pid " ; wait " $pid " # polite stop, then reap
kill -0 " $pid " 2> /dev/null && echo alive # existence check, sends nothing Signal No. Default Trappable Typical source HUP 1 terminate yes terminal closed, "reload" convention INT 2 terminate yes Ctrl+C QUIT 3 core dump yes Ctrl+\ KILL 9 terminate no kill -9, OOM killerTERM 15 terminate yes kill, systemd, Docker, KubernetesSTOP 19 stop no kill -STOPTSTP 20 stop yes Ctrl+Z USR1/USR2 10/12 terminate yes app-defined (log reopen) CHLD 17 ignore yes child exited
Container entrypoints: make the app PID 1 with exec app "$@", or forward signals: app & pid=$!; trap 'kill -TERM $pid' TERM INT; wait $pid; wait $pid.
# getoptsbash Explain Copy
usage () { echo " usage: ${0 ##*/ } -e ENV [-v VERSION] [-n] [-h] [--] [args...] " ; }
env = "" version = latest dry_run = 0
while getopts ' :e:v:nh ' opt ; do # leading ':' = we handle errors; 'e:' = takes a value
case $opt in
e ) env = $OPTARG ;;
v ) version = $OPTARG ;;
n ) dry_run = 1 ;;
h ) usage ; exit 0 ;;
: ) echo " option -$OPTARG requires a value " >&2 ; usage >&2 ; exit 2 ;;
\? ) echo " unknown option -$OPTARG " >&2 ; usage >&2 ; exit 2 ;;
esac
done
shift $(( OPTIND - 1 )) # "$@" is now the remaining positional args
[[ $env =~ ^ ( dev | staging | prod ) $ ]] || { usage >&2 ; exit 2 ; } getopts is a POSIX builtin with short options only. Long options: a manual while (( $# )); do case $1 in --env) env=$2; shift 2 ;; --env=*) env=${1#*=}; shift ;; --) shift; break ;; esac; done. GNU getopt (external) supports long options but is not available by default on macOS.
# grepTask Command Extended regex grep -E 'error|fatal' app.logCase-insensitive, line numbers grep -in 'timeout' app.logInvert (exclude) grep -v '^#' confCount matching lines grep -c ' 500 ' access.logOnly the matching part grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' access.logFixed string, whole line grep -qxF -- "$user" allowed.txtWhole word grep -w 'id' fileQuiet, for if if grep -q 'READY' log; thenRecursive, include glob grep -rn --include='*.yaml' 'image:' k8s/List matching files, NUL-separated grep -rlZ 'old.example.com' /etc/nginxContext lines grep -C 3 'panic' app.log (-A after, -B before)Stop after first match grep -m 1 'Started' app.logTreat binary as text grep -a 'ERROR' app.logPatterns from a file grep -Ff patterns.txt app.logSearch compressed logs zgrep 'req-8f2a' /var/log/app/*.gzPCRE (GNU only) grep -oP 'version=\K[0-9.]+' file
Exit codes: 0 match, 1 no match, 2 error. Under set -e/pipefail, "no match" is a failure unless you handle it.
# sedTask Command Replace all on each line sed 's/old/new/g' fileEdit in place with backup (GNU and BSD) sed -i.bak 's/old/new/g' fileIn place, no backup (GNU) sed -i 's/old/new/g' fileDifferent delimiter for paths sed 's|/usr/local|/opt|g' fileERE and back-references sed -E 's/^([a-z]+)=(.*)$/\2=\1/' filePrint lines 10–20 sed -n '10,20p' filePrint matching lines sed -n '/ERROR/p' fileDelete comments and blank lines sed -e '/^\s*#/d' -e '/^\s*$/d' fileDelete a line range sed '5,10d' fileReplace only on matching lines sed '/^server_name/s/old/new/' fileInsert / append a line (GNU) sed '/^\[main\]/a key=value' file, sed '1i # header' filePrint between two patterns sed -n '/BEGIN CERT/,/END CERT/p' fileStrip CRLF sed -i 's/\r$//' script.shStrip ANSI colour codes sed -E 's/\x1b\[[0-9;]*m//g' logUse a shell variable sed "s/^image: .*/image: ${IMAGE//\//\\/}/" f (escape /, &)
For YAML or JSON, use yq or jq, not sed.
# awkbash Explain Copy
awk ' {print $1, $NF} ' file # first and last field
awk -F: ' $3 >= 1000 {print $1} ' /etc/passwd # field separator + condition
awk -F, -v OFS= ' \t ' ' {print $2, $1} ' in.csv # output separator
awk ' NR==1 {next} {print} ' file # skip header
awk ' NR % 2 == 0 ' file # every second line
awk ' {s += $5} END {print s} ' file # sum a column
awk ' {s += $NF; n++} END {if (n) printf "%.2f\n", s/n} ' file # average
awk ' $9 >= 500 {c[$7]++} END {for (k in c) print c[k], k} ' access.log | sort -rn | head # count by key
awk ' !seen[$0]++ ' file # dedupe keeping order (no sort)
awk ' length > 200 ' file # long lines
awk -v t= " $THRESH " ' $5+0 > t+0 ' file # pass shell vars with -v
awk ' /START/,/END/ ' file # range between patterns
awk ' BEGIN { exit !(4.75 > 4.5) } ' # float comparison as exit status
awk ' { gsub(/[0-9]+ms/, ""); print } ' file # substitute (POSIX: sub, gsub, match, substr, split)
awk -F ' " ' ' {print $2} ' access.log # the quoted request line
awk ' FNR==NR {a[$1]; next} $1 in a ' ids.txt data.txt # join: lines in data whose key is in ids Variable Meaning $0, $1..$NFWhole line, fields NR, FNRLine number overall, per file NFNumber of fields FS, OFSInput / output field separator RS, ORSInput / output record separator FILENAMECurrent file
gensub, asort, strftime and PROCINFO are gawk extensions; Debian/Ubuntu default awk is mawk, Alpine's is BusyBox.
# cut, sort, uniq, tr and friendsTask Command Field by delimiter cut -d, -f2,4 file.csvCharacters cut -c1-10 fileSort numeric, reverse sort -rnSort by column 3 numeric, comma-separated sort -t, -k3,3n file.csvSort human sizes / versions sort -h, sort -VUnique lines sort -uCount duplicates (needs sorted input) sort | uniq -c | sort -rnOnly duplicated / only unique lines uniq -d, uniq -uLines in A not in B (both sorted) comm -23 <(sort a) <(sort b)Join on a key join -t, <(sort -t, -k1,1 a) <(sort -t, -k1,1 b)Translate / delete / squeeze tr 'a-z' 'A-Z', tr -d '\r', tr -s ' 'Count lines / words / bytes wc -l, wc -w, wc -cMerge lines side by side paste -d, a b, paste -sd, list (join lines with commas)Align columns column -t -s,First / last lines, follow head -n 20, tail -n 50, tail -F logReverse lines tac fileShuffle / sample shuf -n 5 fileSplit a big file split -l 100000 -d big.log part_Copy stdout to a file too cmd | tee -a out.logHuman-readable numbers numfmt --to=iec 1073741824 (1.0G)Byte-order sorting, faster and stable LC_ALL=C sort
# findTask Command By name (glob), case-insensitive find /etc -iname '*.conf' -type fNot descending into other filesystems find / -xdev -type f -size +500MModified more than 7 days ago find /var/log -type f -mtime +7Modified in the last 30 minutes find . -mmin -30Newer than a reference file find . -newer /tmp/stampDepth control find . -maxdepth 1 -mindepth 1Prune a directory find . -path ./node_modules -prune -o -name '*.js' -printEmpty files / dirs find . -emptyBy owner / permission find / -xdev -nouser, find / -xdev -perm -4000 -type f (setuid)World-writable files find / -xdev -type f -perm -0002Delete (put -delete last) find /tmp/app -type f -mtime +2 -deleteRun once per batch find . -name '*.log' -exec gzip -- {} +Run once per file find . -name '*.sh' -exec shellcheck {} \;Custom output find . -type f -printf '%TY-%Tm-%Td %s %p\n' (GNU)NUL-separated for pipes find . -type f -print0 | xargs -0 ...Sizes per directory du -xh --max-depth=1 /var | sort -rh | head
Expressions are evaluated left to right: find . -delete -name '*.tmp' deletes everything .
# xargsTask Command NUL-safe find . -name '*.tmp' -print0 | xargs -0 -r rm -f --Do nothing on empty input (GNU) xargs -rN arguments per command xargs -n 1Placeholder xargs -I{} cp {} /backup/Parallel xargs -P 8 -n 1 ./process.shFrom a file xargs -a hosts.txt -P 10 -I{} ssh -n {} uptimeShow commands as run xargs -tCall a shell function export -f fn; xargs -I{} bash -c 'fn "$1"' _ {}
Exit status 123 means at least one invocation failed; 124 means a command exited 255; 125 killed by signal; 126/127 not runnable / not found.
bash Explain Copy
jq . file.json # pretty-print, validate
jq -r ' .items[].metadata.name ' # raw strings, one per line
jq -r ' .items[]? | .name ' # ? = no error if missing/null
jq ' .spec.replicas // 1 ' # default for null/missing
jq -e ' .status == "ok" ' > /dev/null # exit 1 if false/null (for if)
jq -c ' .[] ' # compact, one object per line (JSON Lines)
jq -s ' length ' events.jsonl # slurp JSON Lines into an array
jq ' keys ' # inspect structure: top-level keys
jq ' .items | length ' # count elements
jq ' .items | type ' # array, object, null ...
jq ' .[] | select(.state == "running" and .cpu > 80) '
jq ' map(select(.tags.Owner == null)) | length '
jq -r ' .[] | [.name, .ip, .state] | @tsv ' # TSV (also @csv, @json, @base64, @uri, @sh)
jq -r ' .[] | "\(.name)=\(.value)" ' # string interpolation
jq ' group_by(.zone) | map({zone: .[0].zone, n: length}) '
jq ' to_entries | map("\(.key)=\(.value)") | .[] ' # object -> key=value lines
jq ' .metadata.labels.team = "ops" ' # set a field
jq ' del(.metadata.managedFields) ' # remove a field
jq --arg ns " $NS " ' .items[] | select(.metadata.namespace == $ns) ' # shell string in
jq --argjson n " $COUNT " ' .replicas = $n ' # shell number/JSON in
jq -n --arg msg " $MSG " ' {text: $msg} ' # build JSON safely (Slack payload)
jq -n --rawfile body notes.txt ' {body: $body} ' # file contents as a string
jq -s ' .[0] * .[1] ' base.json override.json # deep merge # yq (mikefarah v4)bash Explain Copy
yq ' .image.tag ' values.yaml # read
yq -i ' .image.tag = "1.4.2" ' values.yaml # edit in place (comments kept)
TAG = 1.4.2 yq -i ' .image.tag = strenv(TAG) ' values.yaml # value from env
yq ' .spec.template.spec.containers[].image ' deploy.yaml
yq ' select(.kind == "Deployment") | .metadata.name ' all.yaml # multi-document files
yq -i ' del(.metadata.annotations) ' deploy.yaml
yq -o=json ' . ' values.yaml | jq ... # YAML -> JSON
yq -P ' . ' file.json # JSON -> pretty YAML
yq ea ' . as $i ireduce ({}; . * $i) ' base.yaml prod.yaml # merge files (eval-all)
yq ' .data | keys ' configmap.yaml
yq --version # check it is mikefarah v4, not python-yq # cron syntaxtext Copy
┌───────── minute 0-59
│ ┌─────── hour 0-23
│ │ ┌───── day of month 1-31
│ │ │ ┌─── month 1-12 or jan-dec
│ │ │ │ ┌─ day of week 0-7 (0 and 7 = Sunday) or sun-sat
│ │ │ │ │
* * * * * command # user crontab (crontab -e)
* * * * * user command # /etc/crontab and /etc/cron.d/* have a USER fieldSchedule Expression Every 5 minutes */5 * * * *Every hour at :17 17 * * * *Daily at 02:30 30 2 * * *Weekdays at 09:00 0 9 * * 1-5Every 15 min, 09:00–17:45, Mon–Fri */15 9-17 * * 1-5First day of the month, 03:00 0 3 1 * *Sundays at 04:00 0 4 * * 0Quarterly 0 0 1 1,4,7,10 *At boot / shortcuts @reboot, @hourly, @daily, @weekly, @monthly
Command Purpose crontab -e, crontab -l, crontab -l -u deployEdit / list / list another user's crontab -rDelete your whole crontab (no confirmation) grep CRON /var/log/syslog, journalctl -u cronDid it run? (crond on RHEL) run-parts --test /etc/cron.dailyWhat cron.daily will run (Debian skips names with dots)
Gotchas: cron uses /bin/sh with PATH=/usr/bin:/bin and no profile; escape % as \%; when both day-of-month and day-of-week are restricted, either matching triggers the job; output is mailed to MAILTO or discarded; files in /etc/cron.d must end with a newline and be root-owned, not writable by others. CRON_TZ= is supported by cronie (RHEL family); check man 5 crontab before relying on it elsewhere.
cron Copy
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAILTO=""
*/5 * * * * opskit flock -n /run/lock/health.lock /opt/opskit/bin/opskit-health >> /var/log/opskit/health.log 2>&1
30 2 * * * root /opt/backup.sh > "/var/log/backup-$(date +\%F).log" 2>&1# systemd timersini Copy
# /etc/systemd/system/job.service
[ Unit ]
Description = Nightly job
OnFailure = notify@%n.service
[ Service ]
Type = oneshot
User = svc
ExecStart = /opt/job/bin/run.sh
EnvironmentFile = -/etc/job/job.env
TimeoutStartSec = 1h
Nice = 10
ProtectSystem = strict
ReadWritePaths = /var/lib/job
PrivateTmp = yes
NoNewPrivileges = yes ini Copy
# /etc/systemd/system/job.timer
[ Unit ]
Description = Run job nightly
[ Timer ]
OnCalendar = *-*-* 02:30:00
RandomizedDelaySec = 15min
Persistent = true
[ Install ]
WantedBy = timers.target OnCalendar=Meaning hourly, daily, weekly, monthlyShorthands (daily = *-*-* 00:00:00) *-*-* 02:30:00Every day at 02:30 *:0/15Every 15 minutes Mon..Fri *-*-* 09:00:00Weekdays at 09:00 Sat *-*-01..07 04:00:00First Saturday of the month (weekday AND date) *-*-01 03:00:00First of the month *-*-* 02:30:00 Asia/KolkataIn a specific time zone (systemd 235+)
Monotonic option Relative to OnBootSec=5minBoot OnUnitActiveSec=10minLast activation of the service (needs a first run: pair with OnBootSec=) OnUnitInactiveSec=10minLast time the service finished OnActiveSec=Timer activation
Command Purpose systemctl daemon-reloadAfter editing unit files systemctl enable --now job.timerEnable the timer , not the service systemctl list-timers --allNEXT / LAST run of every timer systemctl start job.serviceRun now, outside the schedule systemctl status job.serviceLast result, exit status journalctl -u job.service -n 50 --no-pagerLogs of the job journalctl -u job -f, --since "1 hour ago"Follow / time-filter systemd-analyze calendar '*:0/15' --iterations=3Validate a calendar expression systemd-analyze verify job.service job.timerLint unit files systemd-analyze security job.serviceSandboxing score systemd-run --on-calendar='*:0/5' /opt/x.shTransient timer, no files systemctl reset-failed job.serviceClear a failed state
# Locking, timeouts and retriesbash Explain Copy
# One instance at a time
exec 9> /run/lock/job.lock ; flock -n 9 || { echo " already running " ; exit 0 ; }
flock -n /run/lock/job.lock /opt/job.sh # wrapper form (cron)
flock -w 30 /run/lock/job.lock cmd # wait up to 30 s
# Timeouts
timeout 30 cmd # TERM after 30 s, exit 124
timeout --kill-after=5 30 cmd # then KILL after 5 more
curl -fsS --connect-timeout 3 --max-time 10 " $url "
ssh -o ConnectTimeout= 5 -o BatchMode=yes host cmd
read -r -t 10 answer || answer = default
# Retry with exponential backoff and jitter
retry () {
local max = $1 n = 1 ; shift
until " $@ " ; do
(( n >= max )) && return 1
sleep $(( ( 2 ** n ) + RANDOM % 3 )); n =$(( n + 1 ))
done
}
retry 5 curl -fsS " $url " -o out.json
# Wait for a dependency
timeout 120 bash -c ' until pg_isready -h db -q; do sleep 2; done '
timeout 60 bash -c ' until </dev/tcp/db/5432; do sleep 1; done ' 2> /dev/null
# Atomic file replacement (same filesystem)
tmp =$( mktemp " ${ out } .XXXXXX " ); generate > " $tmp " && mv -f -- " $tmp " " $out "
# Bounded parallelism in pure Bash (4.3+)
for h in " ${ hosts [ @ ]} " ; do
(( $( jobs - rp | wc - l ) >= 10 )) && wait -n
check " $h " &
done ; wait # ShellCheck and batsCode Problem Fix SC2086 Unquoted variable: splitting and globbing "$var"SC2046 Unquoted $(...) "$(...)" or mapfileSC2155 local x=$(cmd) masks the exit statusDeclare, then assign SC2164 cd dir without a failure checkcd dir || exitSC2045 Iterating over ls output Use a glob SC2064 Trap string in double quotes expands now Single quotes SC2034 Variable assigned but never used Remove it, or export / # shellcheck disable with reason SC2181 if [ $? -ne 0 ]if ! cmd; thenSC2068 Unquoted $@ "$@"SC2002 Useless cat cmd < fileSC1091 Cannot follow source shellcheck -x, .shellcheckrc source-path=SCRIPTDIRSC2016 $ in single quotes will not expandIntended? add a reasoned disable SC1017 Literal carriage return (CRLF) sed -i 's/\r$//'
bash Explain Copy
shellcheck -x -S warning bin/ * .sh # severity threshold: error, warning, info, style
shellcheck -f gcc bin/ * .sh # compiler-style output for editors and CI
# shellcheck disable=SC2016 # reason goes here, on the same line bash Explain Copy
#!/usr/bin/env bats
# test/backup.bats
setup () {
export PATH = " $BATS_TEST_DIRNAME/stubs:$PATH " # stub aws, curl, systemctl
export STUB_LOG = " $BATS_TEST_TMPDIR/calls.log "
}
@test " fails with exit 2 on unknown option " {
run ./bin/backup.sh -z
[ " $status " -eq 2 ]
[[ $output == * " unknown option " * ]]
}
@test " uploads to the right key " {
run ./bin/backup.sh
[ " $status " -eq 0 ]
grep -q ' s3 cp .* s3://bucket/hosts/ ' " $STUB_LOG "
} bats test/ runs every .bats file; bats --count test/ counts tests; run -127 cmd asserts an exit code (needs bats_require_minimum_version 1.5.0); a bare ! cmd mid-test does not fail it.
# Bash vs POSIX shBash POSIX sh (dash, BusyBox ash) [[ $a == b* ]]case $a in b*) ... ;; esac[[ $v =~ re ]]printf '%s\n' "$v" | grep -Eq 're'arr=(a b), "${arr[@]}"set -- a b, "$@"declare -ANot available: files or key=value lines ${var//a/b}printf '%s' "$var" | sed 's/a/b/g'${var^^}printf '%s' "$var" | tr '[:lower:]' '[:upper:]'$(< file)$(cat file)<(cmd)Temp file or mkfifo cmd &> fcmd > f 2>&1(( i++ ))i=$((i + 1))function f { }f() { }echo -e, $'\t'printf '\t'source file. ./fileread -a, mapfileLoop with read localNot POSIX, but supported by dash and BusyBox set -o pipefailIn POSIX since 2024 (Issue 8) and in BusyBox ash, but dash 0.5.12 (Ubuntu 24.04) fails with set: Illegal option -o pipefail
Check: shellcheck -s sh script, checkbashisms script, and run it with dash script.
# Top 15 interview one-linersbash Explain Copy
# 1. Top 10 client IPs in an access log
awk ' {print $1} ' access.log | sort | uniq -c | sort -rn | head -n 10
# 2. Endpoints returning the most 5xx
awk ' $9 ~ /^5/ {print $7} ' access.log | sort | uniq -c | sort -rn | head
# 3. Biggest directories on this filesystem
du -xh --max-depth=1 / 2> /dev/null | sort -rh | head -n 15
# 4. Delete files older than 14 days, NUL-safe
find /var/log/app -type f -name ' *.gz ' -mtime +14 -print0 | xargs -0 -r rm -f --
# 5. Replace a hostname in every config file that contains it
grep -rlZ ' old.example.com ' /etc/nginx | xargs -0 -r sed -i.bak ' s/old\.example\.com/new.example.com/g '
# 6. Deleted files still holding disk space
lsof -nP +L1 | awk ' NR==1 || $7 > 100*1024*1024 '
# 7. Retry a flaky command 5 times with backoff
for i in { 1..5} ; do curl -fsS " $URL " -o out.json && break ; sleep $(( 2 ** i )); done
# 8. Wait until a port accepts connections (max 60 s)
timeout 60 bash -c ' until </dev/tcp/127.0.0.1/5432; do sleep 1; done ' 2> /dev/null
# 9. Pods that are not Running, as namespace/name
kubectl get pods -A -o json | jq -r ' .items[] | select(.status.phase != "Running") | "\(.metadata.namespace)/\(.metadata.name)" '
# 10. Run a command on many hosts, 10 at a time
xargs -a hosts.txt -P 10 -I {} ssh -n -o ConnectTimeout=5 {} 'uptime' 2>&1
# 11. Days until a TLS certificate expires
end =$( echo | openssl s_client -connect example.com:443 -servername example.com 2> /dev/null | openssl x509 -noout -enddate | cut -d= -f2 ); echo $(( ($( date -d " $end " +%s ) - $( date +%s )) / 86400 ))
# 12. Compare installed packages on two servers
diff <( ssh web1 ' dpkg-query -W -f="\${Package}\n" ' | sort ) <( ssh web2 ' dpkg-query -W -f="\${Package}\n" ' | sort )
# 13. HTTP status distribution of 50 requests
for i in { 1..50} ; do curl -s -o /dev/null -w ' %{http_code}\n ' " $URL " ; done | sort | uniq -c
# 14. Run a job only if no other copy is running
flock -n /run/lock/backup.lock /opt/backup.sh || echo " skipped: already running "
# 15. Follow a log and alert on a pattern, line by line
tail -Fn0 /var/log/app/app.log | grep --line-buffered ' OutOfMemory ' | while read -r line ; do logger -t oom-watch " $line " ; done