LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash scripting, separating variable from string text (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-separating-variable-from-string-text-735603/)

emmalg 06-25-2009 01:27 PM

Bash scripting, separating variable from string text
 
Hi All

I remember there is a whay to do this with brackets, but I can't remember exactly how and can't come up with the right search term to google it!

I have:

$var1=some
$var2=stuff

I want to get:

some_stuff

but

$var1_$var2

wont work as it alays thinks the underscore is part of the variable name.

Whatever I do, it has to print correctly with echo and most importantly be correctly passed to another shell command I'm trying to execute.

Cheers
Emma

David the H. 06-25-2009 01:59 PM

The long form of the variable is ${var}.

${var1}_${var2}

There's a handy reference page of bash commands at the ABSG here.

You can also escape or quote parts of the string that you want to be seen as literal. The following will all work:

$var1\_$var2
$var1"_"$var2
$var1'_'$var2

Edit after testing: Except that they don't always not work when surrounded by other quotes in an echo command. For that, try echo "$foo"_"$bar".

druuna 06-25-2009 01:59 PM

Hi,

Use curly brackets:

${var1}_${var2}

Hope this helps.

emmalg 06-26-2009 04:31 AM

Thank you both very much!!

Sometimes there's too much information on the internet to find what you need! It's really good to have a forum like this at those times!

Cheers
Emma


All times are GMT -5. The time now is 04:47 PM.