Linux - SoftwareThis 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.
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.
I want to create a loop to show the content of these variables:
for (( X = 1; X <= 3; X++ ))
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?
As an alternative approach instead of using arrays, you can try something like:
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
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.
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:
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.