Technical skill

Bash syntax: Expansions

A command often needs a value that can change, such as a filename, a number, or the words someone typed after the command. Bash is a program that reads commands and prepares their arguments before starting another program. An argument is one piece of input passed to that program. Bash can fill in shortcuts during this preparation; those substitutions are called expansions. For example, {draft,final}.txt becomes two arguments, draft.txt and final.txt. The order of these substitutions explains why quoting a value can change what a command receives.

Bash performs several kinds of expansion in a set order. Brace expansion happens first. Tilde, parameter and variable, arithmetic, and command expansions follow; process substitution, when supported, happens alongside those. Word splitting and filename expansion then occur, followed by quote removal. Brace expansion happens before variable expansion, so {1..3} produces a sequence of words, while $name expands a variable. Text produced by one expansion is not normally reread as fresh shell syntax.

Parameter expansion can provide a default. ${name:-guest} uses guest when name is unset or empty; ${name-guest} uses it only when the variable is unset. Neither form assigns the fallback to the variable. The colon changes the condition, so choose the form based on whether an empty value is meaningful.

Arithmetic expansion evaluates an integer expression:

count=4
next=$((count + 1))
printf 'Next: %s\n' "$next"

Command substitution runs a command and replaces the expression with its standard output. Bash removes trailing newline characters from that output. $(command) is easier to nest than legacy backticks. The output remains data; it is not executed again as shell code unless code explicitly asks Bash to parse it with eval.

After unquoted expansions, Bash can split words using IFS and then expand filename patterns. Unquoted *, ?, and [ have pattern meanings; when used as data, quote them. A scalar such as "$path" remains one argument even if the path has spaces. Arrays preserve argument boundaries as well:

files=(report one.txt 'report two.txt')
printf '<%s>\n' "${files[@]}"

The quoted form "${files[@]}" expands each element separately. This matters when a filename contains spaces. Double quotes still permit parameter, command, and arithmetic expansion; single quotes keep every enclosed character literal.

The special parameter "$@" expands positional arguments as separate words and is the usual way to forward them: some_command "$@". Unquoted $@ can undergo splitting and filename expansion, losing the boundaries supplied by the caller.

For an unexpected argument, use printf '%s\n' "$value" in a small test script to print the value clearly. set -x shows expanded commands, which can reveal extra words, but a trace may expose passwords or tokens. Keep traces out of shared logs and disable them when the investigation is done.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful