Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Concantenation Problem 2

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello,

In creating a Bourne shell script I can easily set a variable using the below:
Code:
VAR1="A"
VAR2="B"
VAR3="$VAR1.$VAR2"
echo $VAR3

Output: [b][COLOR=blue]A.B[/color][/b]

The problem I am having is when I try to use an underscore. The below does not work:
Code:
VAR1="A"
VAR2="B"
VAR3="$VAR1_$VAR2"
echo $VAR3

Output: [b][COLOR=blue]B[/color][/b]

Desired output: A_B

How can I use an underscore in a variable value when concantenatiing two together?

Thanks,

Michael42
 
Use braces around var names:

VAR3="${VAR1}_${VAR2}"


_ is also a valid character in a var name, so your script was looking for the value of var VAR1_, which resulted in a null string.



HTH,

p5wizard
 
VAR3="${VAR1}_$VAR2"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks guys - you always have the answer. I have seen the curly brackets used but never understood them until now. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top