Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-13-2007, 09:29 AM
|
#1
|
|
LQ Newbie
Registered: Dec 2002
Location: Brazil
Posts: 26
Rep:
|
Bash variable string expansion
Hi, folks!
Suppose I have the following variables:
VAR1="STRING1"
VAR2="STRING2"
VAR3="STRING3"
I want to create a loop to show the content of these variables:
for (( X = 1; X <= 3; X++ ))
do
echo "$VAR$X"
done
This returns me "1", "2" and "3" instead of "STRING1", "STRING2" and "STRING3". I know I'm doing something wrong, but I can't figure what. I read at bash man pages something about variable expansion, but couldn't find the corret way to make this work. Can someone help me?
Thanks in advance!
Reginald0
|
|
|
|
02-13-2007, 09:49 AM
|
#2
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,271
|
You are trying to use the $X variable as an array subscript, so consult the bash manual on the subject: http://tldp.org/LDP/Bash-Beginners-G...ect_10_02.html
--- rod.
|
|
|
|
02-13-2007, 10:11 AM
|
#3
|
|
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:
|
As an alternative approach instead of using arrays, you can try something like:
Code:
IFS=" \t\n" #this is usually the default, makes $mySTRINGS be split on spaces, tabs and newlines in the
#for-loop. IFS should thus at least contain " " (space) to split the strings on the spaces...
mySTRINGS="STRING1 STRING2 STRING3"
for str in $mySTRINGS; do
echo $str
done;
Your approach failed simply because $VAR is empty/undefined and your echo interpreted $VAR and $X as separate variables, not as the variable $VARn with n=1,2,3.
|
|
|
|
02-13-2007, 10:18 AM
|
#4
|
|
LQ Newbie
Registered: Dec 2002
Location: Brazil
Posts: 26
Original Poster
Rep:
|
Quote:
|
Originally Posted by theNbomr
|
theNbomr and timmeke, you're both correct, but in fact this is not my case. I would like to know how to expand the content of a variable using another variable to reference it's name, being an array or not, like in this case:
VAR1="STRING1"
X="1"
echo "$VAR$X"
This returns "1" instead of "STRING1" since $VAR doesn't exist. My intention is: echo the content of "$VAR1" using $X as parameter. I don't know if I'm being clear, let me know.
|
|
|
|
02-13-2007, 10:19 AM
|
#5
|
|
ReliaFree Maintainer
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware, Cross Linux from Scratch, Gentoo
Posts: 2,663
Rep: 
|
Works on my box and keeps your general approach...
Code:
#!/bin/bash
VAR1='STRING1'
VAR2='STRING2'
VAR3='STRING3'
for (( X=1; X <= 3; X++ ))
do
eval Y=\$$"VAR""$X"
echo $Y
done
Last edited by weibullguy; 02-13-2007 at 10:20 AM.
|
|
|
|
02-13-2007, 10:38 AM
|
#6
|
|
LQ Newbie
Registered: Dec 2002
Location: Brazil
Posts: 26
Original Poster
Rep:
|
Quote:
|
Originally Posted by Arow
Works on my box and keeps your general approach...
Code:
#!/bin/bash
VAR1='STRING1'
VAR2='STRING2'
VAR3='STRING3'
for (( X=1; X <= 3; X++ ))
do
eval Y=\$$"VAR""$X"
echo $Y
done
|
Arow, you're absolutely right, I already tried the eval command to display the content, but forgot to escape the first $ with \. Now I did this way:
echo "$(eval echo "\$VAR$X")"
Thanks a lot all guys for your attention!
Reginald0
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:36 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|