POSIX Shell: Referencing variables

📄 OneThingWell.dev 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}).


Comments and suggestions

You can use this form to submit a suggestion or a comment.

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