OnCallReadyStart learning

Bash Scripting: glossary

62 terms, in plain words, with where each one is taught.

[[ ]] (double brackets)
Bash's test syntax: [[ -f $f ]] (is a file), [[ $a == b ]], [[ $x =~ regex ]]. It exits 0 for true, and does not word-split inside. Lesson 6.10
acceptance test (acceptance tests)
A script that runs another script with realistic, awkward inputs and prints PASS or FAIL for each requirement.
arithmetic ($(( )), (( )), arithmetic expansion)
Integer maths in bash: $((n + 1)) gives the result as text, (( n > 5 )) is a true/false test. Gotcha: (( )) counts as a failure when its result is 0, which set -e treats as an error. Lesson 6.10
array (arrays, indexed array)
One variable holding a numbered list of values: hosts=(web1 web2). ${hosts[0]} is the first, "${hosts[@]}" all of them. Lesson 6.3
artifact (artifacts, build artifact)
The file a build produces - the packaged program that then gets copied (shipped) to where it runs.
associative array (associative arrays, declare -A)
An array indexed by names (keys) instead of numbers - a lookup table: count[payments]=2. In bash it needs declare -A first.
BASH_REMATCH
The array where bash puts the results of the last [[ $x =~ regex ]]: index 0 the whole match, 1 the first capture group, and so on. Lesson 6.10
build (builds, build step)
Turning source code into the file that gets shipped (the artifact).
capture group (capture groups)
A part of a regex in parentheses; the text it matched is saved so you can use it afterwards (BASH_REMATCH[1] in bash, \1 in sed). Lesson 6.10
case statement (case ... esac, esac)
A bash statement that compares one value against a list of glob patterns and runs the branch of the first match; it ends with esac. Lesson 6.5
checksum (checksums, sha256sum)
A short fingerprint computed from a file's bytes. If a single byte differs, the checksum differs, so it proves two files are identical.
cleanup (clean up)
The part of a script that removes what it created (temp files, lock files) - ideally in a trap, so it runs however the script ends. Lesson 6.15
condition (conditions)
The command after if or while. Its exit status decides the branch: 0 means true. set -e does not fire on a failing condition. Lesson 6.3
CSV (comma-separated values)
Text with one row per line and fields separated by commas - what spreadsheets export. Lesson 6.8
dash
A small, fast shell that Ubuntu uses as /bin/sh. It lacks many bash features (arrays, [[ ]], $'...'), so bash scripts must say bash in their shebang. Lesson 6.1
delimiter (delimiters, separator)
The character that separates fields in a line (: in /etc/passwd, , in CSV), or the marker word that ends a here-document. Lesson 6.8
element (elements)
One value inside an array (or a JSON list). Lesson 6.12
escape character (escape characters, escaping, backslash escape)
A character, usually a backslash, that changes the meaning of the one after it: \n is a newline, \. a literal dot. Lesson 6.8
exec redirection (exec >, exec >>)
exec with only redirections, e.g. exec >> log 2>&1, applies them to the script itself, so everything after it goes to the log. Lesson 6.17
expansion (expanded, expands to)
The shell replacing something like $name, $(cmd) or *.log with its value before the command runs. Lesson 6.1
false positive (false positives)
A warning that is wrong for your case. Silence it narrowly with a # shellcheck disable=SC.... comment just above the line. Lesson 6.22
field (fields)
One piece of a line of text, between delimiters. In /etc/passwd the user name is field 1 and the UID field 3. Lesson 6.8
fork (forking, forks)
Starting a new process. Calling a program like basename forks; a parameter expansion does not, which is why it is faster in a loop. Lesson 6.12
format string (printf format)
The first argument to printf, saying how to print the rest: %s a string, %d an integer, \n a newline. Lesson 6.5
getopts
A bash builtin that reads a script's options (-e prod -n) one at a time in a while loop, putting each letter in a variable. Lesson 6.20
global variable (global variables)
A variable visible in the whole script. Every variable is global unless a function declares it local. Lesson 6.15
greedy (greedy match, longest match)
Matching as much text as possible. In ${p##*/} the doubled ## is the greedy form; in regex * and + are greedy. Lesson 6.12
here-document (heredoc, heredocs, here-doc)
Lines written right in a script and fed to a command as its input, up to a marker line such as EOF. With the marker quoted, $ inside stays literal. ``bash cat > notes.txt <<'EOF' line one EOF ``
here-string (<<<)
cmd <<< "text" feeds the text, plus a newline, to cmd as its input. Lesson 6.8
IFS (internal field separator)
The characters bash cuts unquoted values into words on (normally space, tab, newline). Scripts often set it to just newline and tab. Lesson 6.1
index (indices, indexes)
The position of an element in an array, counting from 0: ${hosts[0]} is the first. Lesson 6.12
local variable (local variables, local x)
A variable declared with local inside a function exists only in that function, so it cannot clash with the rest of the script. Gotcha: local x=$(cmd) hides cmd's exit status. Write local x then x=$(cmd). Lesson 6.3
mktemp (temp directory, temporary file)
A command that creates a new temp file (or with -d a directory) with a random name only you can read, and prints its path. Lesson 6.15
no-op (:)
A command that does nothing. In bash it is :, used so an expansion like : "${VAR:?}" gets evaluated. Lesson 6.12
noclobber (set -C)
A bash option (set -C) that makes > refuse to overwrite an existing file; >| overrides it. Lesson 6.17
OPTARG
The variable where getopts puts the value of an option that takes one (the prod in -e prod). Lesson 6.20
OPTIND
The position of the next argument getopts will read. shift $((OPTIND - 1)) drops the options it has already handled. Lesson 6.20
optstring (option string)
The list of letters given to getopts, e.g. ":e:t:nh". A letter followed by : takes a value; a leading : means "I will handle errors myself". Lesson 6.20
parameter expansion (${var:-default}, ${var:?}, ${p##*/})
Operators inside ${ } that change a variable's value as it is expanded: defaults (:-), required (:?), trimming (# %), replacing (//). Lesson 6.12
pipefail (set -o pipefail)
A bash option that makes a pipeline fail if any of its commands fails, not only the last one. bash -c 'false | true; echo $?' prints 0; add set -o pipefail; at the front and it prints 1. Lesson 6.1
PIPESTATUS
A bash array holding the exit status of every command in the last pipeline, first command at index 0. Lesson 6.3
positional parameter (positional parameters, $1, $@)
The arguments a script or function was given: $1 the first, $2 the second, "$@" all of them, $# how many. Lesson 6.6
POSIX
The standard describing how Unix-like systems and their shells should behave. #!/bin/sh scripts are meant to stick to it.
post-incident (post-incident review)
Looking back at an incident once it is over, to find out exactly why it happened and fix the cause.
process substitution (<(cmd), < <(cmd))
<(cmd) makes a command's output look like a file. done < <(cmd) feeds a loop from a command while keeping it in the current shell. Lesson 6.8
PS4
The text bash prints at the start of every trace line (default + ). Adding file and line number makes traces easier to follow. Lesson 6.22
quoting (double quotes, single quotes)
Putting quotes around text so the shell treats it as one piece. Double quotes still expand $var; single quotes keep everything exactly as typed. Lesson 6.6
release (releases)
Building a new version of a program and putting it into use. A release script automates the steps.
SC code (SC2086, SC2155, SC2164)
shellcheck's number for a kind of mistake, e.g. SC2086 = unquoted variable, SC2155 = local x=$(cmd). Lesson 6.22
set -e (errexit, set -euo pipefail)
A bash option that makes a script stop as soon as a command fails, instead of carrying on. It has four blind spots (see 6.3). Lesson 6.1
set -u (nounset)
A bash option that turns using an unset variable into an error, so a typo in a variable name stops the script instead of expanding to nothing. Lesson 6.1
shellcheck
A tool that reads a shell script without running it and reports likely bugs, each with a code like SC2086. Install with sudo apt install shellcheck, run with shellcheck script.sh. No output means no findings. Lesson 6.3
spool (spool directory, upload spool)
A folder where files wait until something processes them, like uploads waiting to be handled.
static analysis
Checking code for mistakes by reading it, without running it. shellcheck is static analysis for shell scripts. Lesson 6.22
subshell (subshells)
A copy of the shell running as a child process. Changes it makes (cd, variables) vanish when it ends. ( ), $(...) and each side of a pipe run in one. Lesson 6.5
syntax (syntax error)
The grammar of a language. A missing fi or done is a syntax error; bash -n checks only the syntax. Lesson 6.22
trap (traps, EXIT trap, trap cleanup EXIT)
trap COMMAND EVENT tells bash to run COMMAND when EVENT happens: EXIT (the script ends, for any reason), INT (Ctrl+C), TERM (a polite kill). SIGKILL (kill -9) cannot be trapped, so no cleanup runs for it. Lesson 6.15
unset variable (unset, unbound variable)
A variable that was never given a value. By default it silently expands to nothing; with set -u using it is an error ("unbound variable"). Lesson 6.1
usage message (usage messages)
The short help a script prints when called wrong, e.g. usage: deploy.sh -e ENV [-n]. By convention it goes to stderr, with exit status 2. Lesson 6.20
word splitting (word-split, word-splits, split into words)
The shell cutting an unquoted value into separate arguments at spaces, tabs and newlines. Double quotes prevent it. Lesson 6.6
wrapper (wrapper script)
A small script or function that adds something (a log line, a default) and then runs another command with the same arguments. Lesson 6.6
xtrace (bash -x, set -x)
Bash printing each command, after expansion, just before it runs (lines starting with +), so you see what a script really did. Lesson 6.22