Bash Shell and scripting
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
cmd > <file>
cmd >> <file>
cmd1 | cmd2cmd 2>&1"$var"$(cmd)cmd1 && cmd2
Ctrl+RConcepts
- Read line split into words
- Expansions braces, $var, $(cmd), globs
- Redirections stdin, stdout, stderr
- Command exit status in $?
Redirection & Pipes
cmd > <file>
cmd >> <file>
cmd < <file>
cmd1 | cmd2cmd 2> <file>
cmd > <file> 2>&1
cmd &> <file>
cmd > /dev/null 2>&1
cmd | tee <file>
cmd <<< "<text>"
diff <(cmd1) <(cmd2)
cmd |& cmd2cmd <<'EOF'
exec > <file>
Quoting & Expansion
"$var"'$var'$(cmd)*.txtfile?.txt{a,b,c}{1..10}$((a + b))~ / ~user\<char>$'a\tb'**/*.txtVariables
name=<value>
export name=<value>
$1 $2 ... $@$?$#$$ / $!${var:-<default>}
${var:=<default>}
${#var}${var#<pat>} / ${var%<pat>}
${var/<pat>/<new>}
${var^^} / ${var,,}
arr=(a b c)"${arr[@]}"
declare -A maplocal name=<value>
Control Flow
if cmd; then ...; fi
[[ -f <file> ]]
[[ "$a" == "$b" ]]
[[ $n -lt 5 ]][[ -z "$s" ]] / [[ -n "$s" ]]
for x in a b c; do ...; done
for f in *.txt; do ...; done
for ((i=0; i<n; i++)); do ...; done
while read -r line; do ...; done < <file>
case $x in a) ...;; *) ...;; esac
cmd1 || cmd2name() { ...; }break / continue
[[ $s =~ <regex> ]]
Command Line (Readline)
!!!$history | grep <text>
^<old>^<new>
Jobs & Processes
cmd &jobsbg / fgkill <pid>
waitnohup cmd &cmd1; cmd2time cmdSafe Scripts
#!/usr/bin/env bash
set -euo pipefail
trap '<cmd>' EXIT
set -xshellcheck <script>
source <file> / . <file>
read -r -p "<prompt>" var
command -v <name>
Recipes
#!/usr/bin/env bashset -euo pipefailtmp=$(mktemp)trap 'rm -f "$tmp"' EXIT# work with "$tmp"
for f in *.jpeg; do mv -- "$f" "${f%.jpeg}.jpg"done
while IFS= read -r line; do echo "got: $line"done < input.txt
./job.sh > run.log 2>&1 &tail -f run.log
Help
help <builtin>
man bashtype <name>



