01 / GETTING STARTED · LESSON 03

Variables and environment

Distinguish shell variables from values exported to child processes.

13 min · learn at your pace

Assign a value, then expand it

Write role=operator with no spaces around =. role = operator asks Bash to run a command named role. Read a variable with $role or ${role}; braces clarify where its name ends. Quote expansions when they should become one argument.

A shell variable belongs to the current Bash process. export adds it to the environment inherited by child programs. A child cannot change the parent’s variables. That boundary is useful when a wrapper supplies configuration to a script.

Work with explicit defaults

${1:-lab} uses the first script argument, or lab when it is missing or empty. This is available in Bash 3.2. A command such as bash -c 'printf ... "$REGION"' starts a fresh child: the outer single quotes delay expansion until that child reads its own environment.

Avoid storing passwords in lesson code or the environment. This exercise uses fictional deployment labels. Your normal terminal session persists until Stop, Reset or a fresh Check; Run creates a child script rather than sourcing it into the parent.

Try an example.sh
team='operations'
export REGION='west'
printf 'team=%s\n' "$team"
bash -c 'printf "region=%s\\n" "$REGION"'
YOUR TURN

Put it into practice.

Set a shell variable `label` to the first argument, defaulting to `lab`. Export it as `LAB_LABEL`. Print `local=<label>` from this script and `child=<label>` from a child Bash process, one line each. Preserve spaces and Unicode.

See an explained solution
One way to solve it.sh
label=${1:-lab}
export LAB_LABEL="$label"
printf 'local=%s\n' "$label"
bash -c 'printf "child=%s\\n" "$LAB_LABEL"'

Export makes the label visible to the child. Quoted expansions preserve the full value as one argument.