Defining and Referencing Variables
| Since: | 全Linux | |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 1.0(1989) |
Shell variables are defined using the format name=value, and referenced using $name or ${name}. Bash requires no type declarations — all values are treated as strings.
Syntax
Define a variable (no spaces around =).
name=value
Reference a variable.
echo $name
echo ${name}
Delete a variable.
unset name
Define a read-only variable.
readonly name=value
Syntax Reference
| Syntax | Description |
|---|---|
| name=value | Assigns a value to a variable. No spaces are allowed around the =. |
| $name / ${name} | References the value of a variable. Curly braces clarify the boundary of the variable name within a string. |
| unset name | Deletes a variable. Any subsequent reference returns an empty string. |
| readonly name=value | Defines a read-only variable. Any subsequent assignment causes an error. |
| declare -i name | Declares a variable as an integer. Arithmetic evaluation is performed on assignment. |
| declare -r name | Declares a read-only variable (equivalent to readonly). |
| declare -a name | Declares an array variable. |
| name=(a b c) | Defines an array. Indices start at 0. |
| ${name[0]} | References the first element (index 0) of the array. |
| ${name[@]} | References all elements of the array. |
Sample Code
Defining and referencing variables. The rule is to never place spaces around the = sign.
sample_variable.sh
name="Ayanami Rei" age=14 echo "Name: $name, Age: $age"
bash variable.sh Name: Ayanami Rei, Age: 14
Use curly braces ${} to clarify the boundary of a variable name. $series_name and ${series}name reference different variables.
sample_brace.sh
series="eva"
echo "${series}_nerv"
bash brace.sh eva_nerv
Use readonly to define a read-only variable. Reassigning it causes an error.
sample_readonly.sh
readonly PI=3.14159 echo "Pi: $PI" PI=3
bash readonly.sh Pi: 3.14159 sample_readonly.sh: line 3: PI: readonly variable
Use unset to delete a variable. After deletion, referencing it returns an empty string.
sample_unset.sh
tmp="temporary data" echo "Before deletion: $tmp" unset tmp echo "After deletion: $tmp"
bash unset.sh Before deletion: temporary data After deletion:
Defining and referencing an array. Indices start at 0.
sample_array.sh
colors=("red" "green" "blue")
echo "First: ${colors[0]}"
echo "All: ${colors[@]}"
echo "Count: ${#colors[@]}"
bash array.sh First: red All: red green blue Count: 3
Use += to append an element to an array.
sample_array_add.sh
colors=("red" "green" "blue")
colors+=("yellow")
echo "After append: ${colors[@]}"
bash array_add.sh After append: red green blue yellow
Common Mistakes
Common Mistake 1: Spaces around = cause a "command not found" error
In Bash, there must be no spaces around = when assigning a variable. A space makes Bash treat the name as a command to execute.
name = "Ayanami Rei" bash: name: command not found
Remove all spaces around the = sign.
name="Ayanami Rei" echo "$name" Ayanami Rei
Common Mistake 2: Confusing $var and ${var} when appending strings
Without curly braces, Bash may not know where the variable name ends. This causes it to look up the wrong variable name entirely.
fruit="apple" echo "$fruits" (empty — Bash looked for a variable named "fruits", not "fruit" + "s")
Use ${var} to clearly mark the variable name boundaries.
echo "${fruit}s"
apples
Notes
When defining shell variables, it is critical that there are no spaces around the = sign. A space causes Bash to interpret the statement as a command (e.g., name = Ayanami Rei means "run the command name with arguments = and Ayanami Rei", which results in an error).
When the boundary of a variable name is ambiguous, use curly braces. For example, $fruits and ${fruit}s reference different variables.
To pass a variable to child processes as an environment variable, use export. For advanced variable expansion such as default values and string manipulation, see Parameter Expansion.
If you find any errors or copyright issues, please contact us.