Variables, Expansion, and Arrays

Keep values intact, transform them deliberately, and control the environment in which commands run.

Assignment and expansion have different syntax

label='east host'
printf 'Host: %s\n' "$label"

Expected: Host: east host

No spaces around = in an assignment. Use $ when expanding a value. Double quotes keep the expanded label one argument.

Choose the default policy

Expression Unset variable Empty variable
${label-default} default Empty value
${label:-default} default default
${label:=default} Assigns default Assigns default

Practice: Which form preserves a deliberately blank optional field?

Observe a required-value failure in a child

bash -c 'commands' starts a child; outer single quotes postpone $ expansion.

unset REQUIRED_LABEL
bash -c 'printf "%s\n" "${REQUIRED_LABEL:?label required}"' 2>diagnostic.txt
status=$?
printf 'status=%s\n' "$status"

Nonzero status and captured diagnostics show the error; the parent continues. Bash versions need not choose the same nonzero number. No test or branch is required here.

Transform text without touching files

path='/srv/reports/host.weekly.txt'
file=${path##*/}
printf '%s\n' "${file%.*}" "${file%%.*}"

Expected: host.weekly, then host.

## removes the longest matching prefix. % removes the shortest matching suffix; %% removes the longest. These are shell patterns, not regular expressions.

Compute quantities as integers

files=10
printf 'groups=%s; remainder=%s\n' "$((files / 3))" "$((files % 3))"

Expected: groups=3; remainder=1

Integer division discards the fractional part. Zero whole MiB does not imply zero bytes. Use trusted numeric inputs; later lessons develop validation.

Preserve array element boundaries

reports=('host one.txt' '*.txt' '')
printf 'count=%s\n' "${#reports[@]}"
printf '<%s>\n' "${reports[@]}"

Expected: count 3; arguments <host one.txt>, <*.txt>, <>.

Quoted @ preserves each element, including an empty element.

Removing an element leaves a gap

reports=('east.txt' 'west.txt')
reports+=('north host.txt')
unset 'reports[1]'
printf 'count=%s; index2=%s\n' "${#reports[@]}" "${reports[2]}"

Expected: count=2; index2=north host.txt

Element count is not the greatest index plus one. Associative arrays require Bash 4+; required examples also work on Bash 3.2.

A child receives a copy

REPORT_LABEL=original
export REPORT_LABEL
bash -c 'REPORT_LABEL=revised; printf "child=%s\n" "$REPORT_LABEL"'
printf 'parent=%s\n' "$REPORT_LABEL"

Expected: child=revised, then parent=original.

A child assignment does not update its parent. A command-prefix assignment can limit an environment value to one invocation.

PATH is an ordered trust decision

The lesson creates two harmless fixture programs with the same name.

Child PATH order Selected fixture
trusted, then untrusted trusted
untrusted, then trusted untrusted

The experiment changes only the child's PATH. Explicit paths select a file; ownership and directory permissions still determine who can replace it.

Install your own command in ~/bin

mkdir -p ~/bin
cp hostcard ~/bin/ && chmod 755 ~/bin/hostcard
command -v hostcard    # not found until a new login

Ubuntu's ~/.profile adds ~/bin to PATH at login, if it exists.
Aliases go in ~/.bash_aliases; only interactive shells read them, so scripts use the command name.

Verify before extending

Use the complete temporary-directory setup in each lesson.

  • Check unset and deliberately empty values separately.
  • Test filenames with spaces and literal wildcard characters.
  • Compare parent and child environments using synthetic labels.
  • Keep real credentials and startup files outside experiments.

Then complete the graded lab walkthrough on your VM. Record commands, results, and assumptions.

Source context: Linuxize parameter expansion, arrays, and startup-file articles linked in the lessons.

Point out ~/bin comes first in PATH, so the trust rule from the previous slide applies: keep it writable only by the owner.