LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 02-13-2007, 09:29 AM   #1
Reginald0
LQ Newbie
 
Registered: Dec 2002
Location: Brazil
Posts: 26

Rep: Reputation: 15
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
 
Old 02-13-2007, 09:49 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 02-13-2007, 10:11 AM   #3
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 02-13-2007, 10:18 AM   #4
Reginald0
LQ Newbie
 
Registered: Dec 2002
Location: Brazil
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by theNbomr
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.
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.
 
Old 02-13-2007, 10:19 AM   #5
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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.
 
Old 02-13-2007, 10:38 AM   #6
Reginald0
LQ Newbie
 
Registered: Dec 2002
Location: Brazil
Posts: 26

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


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
bash search for a pattern within a string variable nutthick Programming 8 03-17-2015 07:26 PM
Bash variables expansion olaola Linux - Newbie 4 10-16-2006 11:45 AM
Variable expansion inside of a bash script! A.S.Q. Linux - Newbie 4 09-29-2006 09:09 AM
Bash Script Expansion Problem meadensi Linux - Newbie 1 03-04-2005 05:17 PM
bash script $n variable expansion cortez Programming 6 12-08-2003 04:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:42 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
Open Source Consulting | Domain Registration