{"dataset":{"id":"00000000-0000-0000-0000-000000000044","slug":"bash-best-practices","name":"Bash best practices","description":"Source-linked guidance on Bash scripting fundamentals: quoting, shell options with set, conditional constructs, parameter expansion, and arrays. Scoped to Bash as documented in the GNU Bash Reference Manual; not a general POSIX sh corpus.","visibility":"public","version":1,"documentCount":5,"publisher":"Koskamo editorial","license":"Original summaries; linked sources retain their own terms.","reviewedAt":"2026-10-04T00:00:00.000Z","readOnly":true,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},"total":5,"documents":[{"title":"Indexed and associative arrays","sourceUrl":"https://www.gnu.org/software/bash/manual/html_node/Arrays.html","text":"Bash provides one-dimensional indexed and associative array variables: any variable may be used as an indexed array, declare -a name declares one explicitly, and declare -A name creates an associative array. There is no maximum limit on size and no requirement that members be indexed or assigned contiguously. Indexed arrays are zero-based, and a negative subscript counts back from one greater than the maximum index, so -1 references the last element. Assigning name[subscript]=value automatically creates an indexed array; compound assignment name=(value1 value2 ...) assigns many elements at once, and when no subscript is supplied each element takes the last assigned index plus one. Reference elements as ${name[subscript]} — the braces are required to avoid conflicts with filename expansion. A subscript of @ or * expands to all members; double-quoted, ${name[@]} expands each element to a separate word while ${name[*]} joins members into a single word separated by the first character of IFS. ${#name[@]} is the element count and ${!name[@]} expands to the assigned indices. unset name[subscript] removes one element; unset name removes the entire array. Scope: Bash. Reviewed October 4, 2026.","id":"bash-best-practices:Indexed and associative arrays","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Parameter expansion for defaults and errors","sourceUrl":"https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html","text":"The $ character introduces parameter expansion, and braces around the parameter name are optional but protect it from characters immediately following it; braces are required for positional parameters with more than one digit, so ${11} is the eleventh positional parameter while $11 is the first parameter followed by a literal 1. A leading ! introduces indirect expansion, using the value of the named parameter as the name of the parameter to expand. ${parameter:-word} substitutes word when parameter is unset or null, and omitting the colon tests only for unset. ${parameter:=word} assigns word to an unset or null parameter; positional and special parameters may not be assigned this way. ${parameter:?word} writes word to standard error and a non-interactive shell exits with a non-zero status when parameter is null or unset. ${parameter:+word} substitutes word only when parameter is set and not null. In each of these forms word undergoes tilde expansion, parameter expansion, command substitution, and arithmetic expansion. Scope: Bash. Reviewed October 4, 2026.","id":"bash-best-practices:Parameter expansion for defaults and errors","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Conditional constructs: if, case, ((…)), and [[…]]","sourceUrl":"https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html","text":"if executes its test-commands and runs the consequent commands when their return status is zero; elif lists are tried in turn, else runs when no condition tested true, and the return status is that of the last command executed or zero if no condition tested true. case executes the command-list of the first pattern matching its word; clauses end with ;;, ;&, or ;;& — ;& continues into the next clause's command-list, while ;;& retests the next clause's patterns — and a final * pattern is the common idiom for a default case because it always matches. (( expression )) evaluates arithmetic and returns status 0 when the value is non-zero, 1 otherwise. [[ expression ]] evaluates a conditional expression without word splitting or filename expansion on its words, while tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal still occur; with == and != the string to the right is matched as a pattern, = is identical to ==, and < and > sort lexicographically using the current locale. Scope: Bash. Reviewed October 4, 2026.","id":"bash-best-practices:Conditional constructs: if, case, ((…)), and [[…]]","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Shell options with set","sourceUrl":"https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html","text":"The set builtin changes the values of shell options, sets the positional parameters, or, with no options or arguments, displays the names and values of all shell variables and functions in a format reusable as input. set -e exits immediately if a pipeline, list, or compound command returns a non-zero status; the shell does not exit when the failing command is part of a while or until condition, part of an if test, any command in a && or || list except the one following the final operator, any command in a pipeline but the last (subject to pipefail), or a command whose status is inverted with !, and a trap on ERR, if set, is executed before the shell exits. set -u treats expanding unset variables as an error, and a non-interactive shell exits. set -x prints commands to standard error after expansion and before execution, prefixed by the expanded PS4. pipefail, disabled by default, makes a pipeline return the rightmost non-zero status. set -C (noclobber) stops > from overwriting existing files; >| overrides it. set -o prints the current option settings. Scope: Bash. Reviewed October 4, 2026.","id":"bash-best-practices:Shell options with set","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Quoting and word splitting","sourceUrl":"https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html","text":"Quoting removes the special meaning of certain characters or words to the shell: it can disable special treatment for special characters, prevent reserved words from being recognized as such, and prevent parameter expansion. Bash has four quoting mechanisms — the escape character, single quotes, double quotes, and dollar-single quotes. Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \\, and, when history expansion is enabled, !; in POSIX mode ! has no special meaning within double quotes even when history expansion is enabled. Within double quotes a backslash retains its special meaning only when followed by $, `, \", \\, or newline, and backslashes before those characters are removed; backslashes preceding characters without a special meaning are left unmodified. The special parameters * and @ have special meaning when in double quotes. Double-quoting expansions whose results should not be word-split or filename-expanded is Koskamo editorial guidance. Scope: Bash. Reviewed October 4, 2026.","id":"bash-best-practices:Quoting and word splitting","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"}]}