Find the answer to your Linux question:
Results 1 to 4 of 4
Hi all, I'm trying to figure out how to loop a variable inside a variable and print it out. Here is the example for ((i=1;i<=4;i+=1)); do echo $var$i done I ...
  1. #1
    Just Joined!
    Join Date
    Feb 2005
    Posts
    37

    simple bash question

    Hi all,

    I'm trying to figure out how to loop a variable inside a variable and print it out. Here is the example

    for ((i=1;i<=4;i+=1)); do
    echo $var$i
    done


    I can accomplish what I did above by:

    echo $var1
    echo $var2
    echo $var3
    echo $var4

    I've tried many different ways to get this to work to no avail. Any suggestions?

    Jon

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    We pretty much thrashed that to death here.

  3. #3
    Just Joined!
    Join Date
    Feb 2005
    Posts
    37
    thanks. so the answer is that you have to use arrays?

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    No, you probably don't have to use arrays, but that's the best way to write maintainable code.

    If you don't want to use arrays, there's a suggestion in that thread about eval.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...