Skip to main content

Difference between $VAR and ${VAR}?

·138 words·1 min
Linux
Yuriy Semyenkov
Author
Yuriy Semyenkov
DevOps, tech, geek, mentor

This is really a frequent question. I’m explaining in addition to the post(sorry, it is not translated yet) about variable substitution in a file.

There’s no rocket science here, and the difference only lies in how the command interpreter (Bash, zsh, sh, thousands of them) determines the boundaries of the variable name:

$VAR — works well when the variable name is separated from other text by unused symbols, such as a space, or a backslash. But if we insert the variable into a word, it will be interpreted incorrectly:

VAR=eto
echo $VAR      # Will output eto
echo $VARgeek  # Will output the value of VARgeek, if it exists, otherwise — empty

${VAR} — more reliable. Helps to accurately separate the variable name from other text:

VAR=eto
echo ${VAR}     # Will output eto
echo ${VAR}geek # Will output etogeek