Expanding variables

Bash, and shell scripting languages in general, refer to the process of evaluating variables and other parameters such as $1 as parameter expansion.

The safest way to use parameter expansion is to prefix the name of the variable with a dollar sign as part of a double-quoted string:

$ realname='Bash User'
$ printf '%s\n' "Hello, $realname."
Hello, Bash User.

If you need to distinguish the variable name from characters around it, you can use curly brackets:

$ printf '%s\n' "__Hello, ${realname}__"
__Hello, Bash User__

This prevents Bash from trying to expand a variable named realname__, with two trailing underscores.

Note that if you expand a variable that doesn't exist, by default Bash does not issue any warnings; it just ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.