POSIX Shell: Referencing variables

📄 Wiki page | 🕑 Last updated: Feb 17, 2023

The simplest way to reference variables in the shell is to prefix them with the dollar sign, $:

a="My string"
b=42

echo $a
echo $b

Output:

My string
42

Sometimes you'll need to reference the variable inside of another string. You can do that by enclosing the variable in curly brackets:

echo "${a}_points"

Result:

42_points

The syntax ${...} is called parameter expansion, and it can do much more than just referencing the variable. We'll go through that later, but for now, it's important to understand that $ itself starts the parameter expansion and that {} are optional (which makes $a just a shorthand syntax for ${a}).


Ask me anything / Suggestions

If you have any suggestions or questions (related to this or any other topic), feel free to contact me. ℹī¸


If you find this site useful in any way, please consider supporting it.