#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Preserving values with for loop for later use


    This is a unix shell script related question. But posting it here since there is no seperate section for Shell scripts.

    Below is the issue I am having.

    I have a few variables which have certain values in them like
    var1=23
    var2=46
    var3=78 etc...


    I want to save these values with the help of a for loop in a single variable so that I can use it later,beacuse a few lines down the script, some of these variables get set to a different value and I would like to reset them to the values that I had saved.Can you point out a way without using arrays?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    836
    Rep Power
    496
    Hi, recent versions of bash have one-dimensional arrays, e.g.:

    Code:
    var[1]=23
    var[2]=46
    To dereference your values, you'll need curlies around the name of the variable:

    Code:
    echo ${var[1]}
    With other shells, I don't think it is possible, except perhaps by simulating it using symbolic references.

IMN logo majestic logo threadwatch logo seochat tools logo