In POSIX shell, what is the difference between $x and ${x} ?
Example:
#!/bin/sh
x=1
echo $x
echo ${x}
exit 0
They both print "1".
In POSIX shell, what is the difference between $x and ${x} ?
Example:
#!/bin/sh
x=1
echo $x
echo ${x}
exit 0
They both print "1".
man sh
:
The {
and }
are very useful for clarity. It is also very useful for these cases too:-
$10
as actually the first parameter suffixed with a zero. You would need to refer to ${10}
instead.$var_suffix
will not work, but ${var}_suffix
will. The _suffix
will be treated as though it is part of the variable name with the first format, but is just literal text appended after the value of the variable in the latter.The second case might be a bit obscure, so perhaps a demonstration on the command line is better:-
$ a=Hello
$ a_b=world
$ echo $a
Hello
$ echo $a_b
world
$ echo ${a}_b
Hello_b
I hope that this helps and that I haven't confused things.
Robin