Skip to main content
In shell scripting, variable expansion uses the dollar sign ($) to tell the shell to replace the variable name with its stored value.
Running this script:

Braces vs No Braces

You can reference variables with or without braces. Braces become essential when you append characters immediately after the variable name.

With Braces

Without Braces

Both scripts output:

Delimiting Variable Names

Without braces, the shell cannot determine where the variable name ends:

Quoting and Word Splitting

By default, unquoted expansions are split on whitespace defined by IFS (space, tab, newline). Use quotes to preserve the exact value.
The image illustrates how different whitespace characters (tabs, spaces, linebreaks) affect the formatting of a string variable containing "Hello World".

Unquoted Expansion

Quoted Expansion

Always quote expansions when dealing with filenames, paths, or URLs to prevent unintended splitting.

Intentional Splitting

Sometimes you want to iterate over each word in a list:
Quoting the variable in this case treats the entire list as one element:

Best Practices

Use this quick reference to decide when to quote or brace variables:
The image is a slide titled "Expanding Variables" with checkmarks next to "Directory paths, filenames" and "Assigning URLs to variables."
Never rely on unquoted variable expansions for user input or file names—they can introduce security risks or unexpected behavior.

Further Reading

Watch Video