LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash variable string expansion (https://www.linuxquestions.org/questions/linux-software-2/bash-variable-string-expansion-528663/)

Reginald0 02-13-2007 10:29 AM

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

theNbomr 02-13-2007 10:49 AM

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.

timmeke 02-13-2007 11:11 AM

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.

Reginald0 02-13-2007 11:18 AM

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.

weibullguy 02-13-2007 11:19 AM

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


Reginald0 02-13-2007 11:38 AM

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


All times are GMT -5. The time now is 07:51 AM.