Key Takeaways
- Bash variables store shell values. A basic assignment uses
name=valuewith no spaces around the equals sign. $nameand${name}expand a parameter to its value. Braces make boundaries explicit when text follows the variable name.- Double quotes usually preserve a value as one shell word while still allowing expansion. Single quotes suppress ordinary parameter, command, and arithmetic expansion.
- Shell variables and environment variables are related but not identical.
exportmarks a variable for inheritance by child processes. - Command substitution captures command output. The modern form is
$(command). - Expansion is powerful enough to create bugs when values are left unquoted. Quoting variable expansions is a strong default unless you intentionally need word splitting or filename expansion.
OSBash.002 explained paths, the current working directory, and PATH. This lesson moves one level deeper: Bash can store values under names and then substitute those values into commands. That mechanism is called parameter expansion, and it is one of the foundations of practical shell scripting.
The current GNU Bash Reference Manual defines a variable as a named shell parameter with a value and optional attributes. Bash assignment uses the form name=value; a variable can later be expanded with $name or ${name}.
Assign a Variable With name=value
site="bitcoinversus.tech"
track="Open CERT"
lesson=3
The assignment syntax is compact on purpose. Do not insert spaces around = in an ordinary assignment. Bash parses words before it decides what they mean, so site = value is not the same construct as site=value.
# Correct
name="Ada"
# Not a normal variable assignment
name = "Ada"
A shell variable stores text by default. Bash can perform arithmetic and can assign attributes with commands such as declare, but beginners should first master the normal string model: assign a value, expand the value, and quote it correctly.
Expand a Variable With $name
name="Ada"
echo "$name"
The assignment stores Ada. The second line does not pass the literal characters $name to echo; Bash performs parameter expansion and substitutes the variable’s value.
model="S21"
hashrate="200 TH/s"
printf '%s %s\n' "$model" "$hashrate"
printf is often easier to reason about than echo when scripts need predictable formatting. The important point here is that each expanded value is surrounded by double quotes.
Use ${name} When the Boundary Matters
The basic parameter-expansion form documented by GNU Bash is ${parameter}. Braces are optional in many simple cases, but they are valuable whenever the next character could be mistaken for part of the variable name.
name="miner"
echo "$name"
echo "${name}_01"
Without braces, $name_01 asks Bash for a variable named name_01. With ${name}_01, the variable boundary is explicit: expand name, then append _01.
Double Quotes Preserve Expansion Safely
Double quotes allow parameter expansion, command substitution, and arithmetic expansion while preventing ordinary word splitting and filename expansion on the resulting value. This makes "$variable" one of the most important habits in shell scripting.
project="Hash Race"
printf '%s\n' "$project"
Because $project is quoted, the embedded space stays inside a single argument. The GNU Bash quoting documentation explains that quoting removes or preserves the special meaning of shell characters depending on the quoting mechanism.
Single Quotes Mean Literal Text
name="Ada"
echo "$name"
echo '$name'
The first command prints the value of name. The second prints the literal characters $name. Single quotes are useful when you want Bash to stop interpreting most shell syntax inside the quoted text.
Expansion Happens Before a Command Runs
Bash processes shell syntax before it launches an external program. According to the Bash expansion model, parameter expansion, command substitution, and arithmetic expansion happen before later steps such as word splitting, filename expansion, and quote removal. This ordering explains why quoting changes the number of arguments a program receives.
path="$HOME/projects"
printf '%s\n' "$path"
Bash expands $HOME first. The double quotes then preserve the expanded result as a single shell word.
HOME and PATH Are Already Variables
printf '%s\n' "$HOME"
printf '%s\n' "$PATH"
printf '%s\n' "$PWD"
HOME, PATH, and PWD are common shell or environment variables. OSBash.002 used PATH to explain command lookup; now you can see that the same search path is stored as a value that Bash can expand like other parameters.
Shell Variables vs Environment Variables
A variable can exist in the current Bash process without automatically becoming part of the environment inherited by child processes. The GNU Bash manual describes an environment as a list of name=value strings passed to invoked programs. export marks a shell variable so Bash includes it in that environment.
project="Open CERT"
export project
# Equivalent one-line form
export region="us-west"
You can inspect environment entries with commands such as env or printenv. The current Bash documentation also notes that child processes inherit exported values but cannot directly rewrite the parent shell’s variables.
env, PATH, and export.Temporary Environment Values for One Command
Bash also lets you place an assignment directly before a command. That value becomes part of the environment for that command invocation without necessarily changing the variable permanently in the surrounding shell.
LANG=C date
MODE=test ./check.sh
This pattern is common in testing, build systems, deployment scripts, and troubleshooting because it keeps configuration scoped to one command.
Capture Command Output With $(…)
Command substitution runs a command and replaces the substitution expression with that command’s standard output. The modern and readable Bash form is $(command).
today=$(date +%F)
host=$(hostname)
printf 'Date: %s\n' "$today"
printf 'Host: %s\n' "$host"
This turns command output into data your script can reuse. The older backtick form exists, but $(...) is easier to read and nest.
Use Default Values With ${name:-fallback}
Parameter expansion can do more than simple substitution. One highly practical form is ${parameter:-word}, which uses word when the parameter is unset or null.
region="${REGION:-us-west}"
printf '%s\n' "$region"
This pattern is common when scripts accept configuration from environment variables but still need a reasonable default. It is different from permanently assigning the fallback to the original parameter.
Test Whether a Variable Is Set
name=""
printf 'value=[%s]\n' "$name"
unset name
printf 'fallback=%s\n' "${name:-not-set}"
An empty string and an unset variable are not always treated identically. Bash’s parameter-expansion operators can distinguish these states depending on whether the operator includes a colon. Beginners do not need every operator at once, but they should understand that “unset” and “set to an empty value” are separate states.
unset and readonly Change Variable Behavior
name="Ada"
unset name
readonly site="bitcoinversus.tech"
unset removes a variable. readonly marks a value so the current shell cannot normally assign a new value to it afterward. These builtins make intent clearer in larger scripts.
Arithmetic Expansion Is a Different Expansion
count=4
next=$((count + 1))
printf '%s\n' "$next"
$((...)) is arithmetic expansion, not ordinary parameter expansion. Bash permits variable names inside arithmetic expressions without a leading dollar sign. A later lesson can go deeper into arithmetic, tests, and conditional logic; for now, recognize the syntax so you do not confuse it with $(...) command substitution.
A Safe Default: Quote Variable Expansions
When a variable might contain spaces, wildcard characters, an empty value, or user-provided text, unquoted expansion can change how Bash divides the command into arguments. A strong default is to write "$variable" unless you intentionally need Bash to perform word splitting or filename expansion.
file="report october.txt"
printf '%s\n' "$file"
folder="$HOME/My Projects"
printf '%s\n' "$folder"
Quoting is not decoration. It changes shell parsing behavior. This is why reliable scripts treat expansion and quoting as one combined skill.
Common Beginner Mistakes
- Putting spaces around
=: usename=value, notname = value. - Forgetting the dollar sign when reading a value: assignment uses
name=value; expansion uses$nameor${name}. - Using braces incorrectly: write
${name}, not$(name). The latter is command substitution. - Leaving values unquoted by habit: unquoted expansion can trigger word splitting and filename expansion.
- Expecting single quotes to expand variables: single quotes preserve literal text.
- Assuming every shell variable is exported: child processes only inherit variables included in the environment.
- Confusing
$(...)with$((...)): the first captures command output; the second performs arithmetic expansion. - Assuming unset and empty mean the same thing: Bash parameter expansion can distinguish them.
Hands-On Practice
- Create a variable named
nameand print it withprintf. - Create a value containing a space and compare quoted and unquoted expansion.
- Set
model="S21"and build${model}_rack_01using braces. - Print
HOME,PATH, andPWD. - Create a shell variable, then export it and confirm it appears in
env. - Run one command with a temporary environment assignment placed before the command.
- Capture the current date with
today=$(date +%F). - Use
${REGION:-us-west}withREGIONunset, then setREGIONand compare the result. - Use
unseton a test variable and verify its expansion afterward. - Create a small script that combines a variable,
$HOME, and command substitution in one formatted output.
Knowledge Check + Answers
- What is the normal Bash assignment syntax?
name=valuewith no spaces around the equals sign. - What does
$namedo? Performs parameter expansion and substitutes the value associated withname. - Why use
${name}? It makes the parameter boundary explicit and supports additional parameter-expansion forms. - Do double quotes stop parameter expansion? No. They preserve expansion while protecting the result from ordinary word splitting and filename expansion.
- Do single quotes expand
$name? No. They preserve the enclosed characters literally. - What does
exportdo? Marks a shell variable so it is included in the environment inherited by child processes. - What does
$(command)do? Runs the command and substitutes its standard output. - What does
${name:-fallback}mean? Usefallbackwhennameis unset or null. - What is the difference between
$(...)and$((...))? The first is command substitution; the second is arithmetic expansion. - What is a strong default when expanding a variable into a command? Quote it as
"$variable"unless you intentionally need splitting or glob expansion.
Elementary Review
Bash variables are named parameters, and expansion turns those names into values. Assign with name=value, read with $name or ${name}, use double quotes to preserve expanded values safely, use export when child processes need a value, and use $(...) when command output must become data inside the script.
Previous Bash Lessons
Next Bash Lesson
OSBash.004: Quoting, Word Splitting, and Globbing will go deeper into how Bash turns expanded text into command arguments, including spaces, wildcard patterns, quoted versus unquoted values, and safe filename handling.
Primary references: GNU Bash — Shell Parameters, GNU Bash — Shell Parameter Expansion, GNU Bash — Environment, GNU Bash — Quoting, and GNU Bash — Shell Expansions.
BitcoinVersus.Tech Editor’s Note:
We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please donate using the QR code provided

For best QR-code scanning results, please scan this donation code from a desktop or larger screen. Mobile screens may prevent camera recognition.
BitcoinVersus.Tech provides technical education and analysis for informational purposes only.

Leave a Reply