i see some code like this
SERIAL_NO="121"
if [ -z "${SERIAL_NO}" ]; then
echo -e "ERROR: serial number of the device is not provided!"
exit 1
else
here it use ${SERIAL_NO} to got the SERIAL_NO variable. i want know what the difference between $var and ${var} and why use ${var} here.
thanks
2 Answers. There is no difference between echo $var and echo "$var" . However for other commands such as ls (list files) there could be a big difference. The double quotes " tells Linux to treat everything in between as a single entity.
10.3.2. Using the ${#VAR} syntax will calculate the number of characters in a variable. If VAR is "*" or "@", this value is substituted with the number of positional parameters or number of elements in an array in general.
$* Stores all the arguments that were entered on the command line ($1 $2 ...). "$@" Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...).
There actually is a minor difference performance wise. The reason there is a performance difference is because the braces enable the Parameter Expansion (PE) parser. Without the braces the shell knows that no parameter expansion will be performed (as the braces are mandatory for PE). The only reason to use the braces around a variable name when you don't want to perform a PE is to disambiguate the variable name from other text such as var="foo"; echo ${var}name
will output fooname
. Without the braces the shell will try to expand the variable named $varname
which doesn't exist.
There is no difference, if no alphanumeric characters follow. The braces in your example are unneeded.
"$foo42"
is the contents of $foo42
. "${foo}42"
is the contents of $foo
followed by "42".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With