Kolam Ayer MakersLinux Foundations

Variables

Core Idea

Variables name values so scripts can reuse them.

Shell assignment has no spaces around =:

name="Ada Lovelace"
printf 'Hello, %s\n' "$name"

Quotes matter. "$name" preserves the value as one piece of text even when it contains spaces.

Practice Alone

Assign a value, print it with printf, and inspect environment variables with env.

site_title="My Linux Foundations Site"
printf '%s\n' "$site_title"
env | grep '^HOME='

Local variables exist in the current shell. Environment variables are exported to child processes.

local_only="not exported"
export SHARED_WITH_CHILD="exported"
bash -c 'printf "%s\n" "$SHARED_WITH_CHILD"'

Watch Out

Done When

You can tell local variables from environment variables.

Docs Pointers