LinuxQuestions.org
Visit Jeremy's Blog.
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 04-07-2011, 11:36 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
concatenate variable names in bash


Hi,

In my script, and I would like to concatenate 2 variables names, to give me the true variable.

I've 3 variables X1, X2 and X3, and I invoked them inside a for loop.

Code:
#!/bin/bash                                                                                                                                           
X1=HELLO
X2=WORLD
X3=!
for i in {1..3};
do
echo ${X${i}}
done
With the script, I would like to print
Code:
HELLO
WORLD
!
How can I do that?

Thanks
 
Old 04-07-2011, 11:42 AM   #2
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Here's the solution:

Code:
for i in {1..3};
do
    eval "echo \$X$i"
done
 
Old 04-07-2011, 11:55 AM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Now, I would like to put the result in a here-document. How can I do that?

Code:
X1=HELLO
X2=WORLD
X3=!
for i in {1..3};
do
Z=`eval "echo \$X$i"`

cat >> test.txt 
echo $Z
<<EOF                                                                                                                                             
EOF                                                                                                                                                        
done
 
Old 04-07-2011, 12:10 PM   #4
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Well, I found this solution, although I would like to put the result of the eval expression inside the here-document.

Code:
 #!/bin/bash                                                                                                                                                
X1=HELLO
X2=WORLD
X3=!
for i in {1..3};
do
Z=$(eval "echo \$X$i");

cat >> test.txt <<-EOF
$Z
EOF                                                                                                                                                        
done
Does anyone know how to put the eval result inside the here document?
 
Old 04-07-2011, 08:01 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Quote:
although I would like to put the result of the eval expression inside the here-document.
I am not sure I understand you? Are you saying that the $Z value in your here document is not being expanded?

Also, as I try to steer away from eval where possible, you could also do:
Code:
#!/bin/bash                                                                                                                                           
X1=HELLO
X2=WORLD
X3=!

for i in {1..3};
do
    Z="X$i"
    echo ${!Z}
done

Last edited by grail; 04-07-2011 at 08:04 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Basic Bash: How to use eval to evaluate variable names made of arbitrary strings. GrapefruiTgirl Programming 9 12-16-2009 10:25 AM
bash - loop over variable array names talanis Programming 2 02-19-2009 11:09 AM
BASH Using varaibles as variable names SwingingSimian Programming 5 09-02-2008 02:56 AM
Need shell script to concatenate a string and a variable into a directory name AwesomeMachine Linux - Newbie 2 05-07-2006 03:42 AM
how to concatenate variable name in Java ? black Programming 1 11-16-2004 04:08 AM


All times are GMT -5. The time now is 08:28 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration