0

What is the difference between:

./script.sh "$VARIABLE"

and

./script.sh ${VARIABLE}

Is there any?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
sqiero
  • 1
  • 1
    See http://wiki.bash-hackers.org/syntax/quoting or http://www.tldp.org/LDP/abs/html/quotingvar.html. ${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

2 Answers2

1

$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.

MelBurslan
  • 6,966
0

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.

igiannak
  • 750