What is the difference between:
./script.sh "$VARIABLE"
and
./script.sh ${VARIABLE}
Is there any?
What is the difference between:
./script.sh "$VARIABLE"
and
./script.sh ${VARIABLE}
Is there any?
$VARIABLE
and ${VARIABLE}
are effectively the same if they are standalone words. But notice the following example, especially in a script
VARIABLE=USER
echo $VARIABLE
you get output
USER
but when you type
echo $VARIABLE1
expecting to get
USER1
you get nothing as there is no variable as VARIABLE1
defined
But if you use
echo ${VARIABLE}1
you get the expected USER1
output.
from Shawn J. Goff: $VAR vs ${VAR} and to quote or not to quote
VAR=$VAR1 is a simplified version of VAR=${VAR1}. There are things the second can do that the first can't, for instance reference an array index (not portable) or remove a substring (POSIX-portable). See the More on variables section of the Bash Guide for Beginners and Parameter Expansion in the POSIX spec.
Using quotes around a variable as in rm -- "$VAR1" or rm -- "${VAR}" is a good idea. This makes the contents of the variable an atomic unit. If the variable value contains blanks (well, characters in the $IFS special variable, blanks by default) or globbing characters and you don't quote it, then each word is considered for filename generation (globbing) whose expansion makes as many arguments to whatever you're doing.
Provide a link to the original page or answer Quote only the relevant portion Provide the name of the original author
– igiannak Jun 22 '16 at 06:46
${foo}
is the same as$foo
and putting a variable inside double quotes (not necessarily just the variable) will prevent the variable from being split at certain characters like spaces (per default) or globbing to occur (e.g.*
turning into a list of files). – phk Jun 21 '16 at 18:57