LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-25-2009, 12:27 PM   #1
emmalg
Member
 
Registered: Jun 2009
Location: Spain
Distribution: Various, Ubuntu, Fedora, Open Solaris, Solaris, RHEL, CentOS
Posts: 64

Rep: Reputation: 16
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
 
Old 06-25-2009, 12:59 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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".

Last edited by David the H.; 06-25-2009 at 01:07 PM.
 
Old 06-25-2009, 12:59 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Use curly brackets:

${var1}_${var2}

Hope this helps.
 
Old 06-26-2009, 03:31 AM   #4
emmalg
Member
 
Registered: Jun 2009
Location: Spain
Distribution: Various, Ubuntu, Fedora, Open Solaris, Solaris, RHEL, CentOS
Posts: 64

Original Poster
Rep: Reputation: 16
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
 
  


Reply

Tags
bash, script, shell, variables



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
bash scripting need help getting a variable of a variable dovo Programming 6 10-29-2008 01:30 PM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 10:38 AM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration