Defining and Referencing Variables
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.
variable.sh
name="Alice"
age=30
echo "Name: $name, Age: $age"
bash variable.sh
Name: Alice, Age: 30
Use curly braces ${} to clarify the boundary of a variable name. $fruitsa and ${fruit}s reference different variables.
brace.sh
fruit="apple"
echo "${fruit}s are delicious"
bash brace.sh
apples are delicious
Use readonly to define a read-only variable. Reassigning it causes an error.
readonly.sh
readonly PI=3.14159
echo "Pi: $PI"
PI=3
bash readonly.sh
Pi: 3.14159
bash: PI: readonly variable
Use unset to delete a variable. After deletion, referencing it returns an empty string.
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.
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.
array_add.sh
colors=("red" "green" "blue")
colors+=("yellow")
echo "After append: ${colors[@]}"
bash array_add.sh
After append: red green blue yellow
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 = Alice means "run the command name with arguments = and Alice", 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.