Pages

Thursday, September 27, 2012

Difference between $variable and ${variable} in (bash) shell scripting

Many often we do shell scripting, but small small ignorance at times cost a lot. Here is an example of what we mean and what it become.
[/tmp]$ cat try.sh #! /bin/sh a=12 b=34 echo $a_$b echo ${a}_$b [vivarkey@/tmp]$ sh try.sh 34 12_34 [/tmp]$


In the above shell script we intended to print "variableA_variableB", in the shell script on last but one line. But since we missed the braces around the variable it actually truncated the previous variable along with the '-' symbol.

NB: This code snippet is tried in bash shell on Linux, on mac it gives different result.

HTH
Kongkon

No comments: