You are not logged in.

#1 2009-11-24 04:53:08

Mardoct
Member
Registered: 2009-08-17
Posts: 208

Bash Question [Answered]

Ok, quick question that Google has failed to answer.

I have a for loop in which each $i is the name of a variable that I defined before. If the user wants to. They can change it from the default value specified before. The problem is that I can edit the variable with $i=$input . I there any way to get around Bash's prejudice against variable assaignment of expanded names.

Last edited by Mardoct (2009-11-24 05:45:33)


The human being created civilization not because of willingness but of a need to be assimilated into higher orders of structure and meaning.

Offline

#2 2009-11-24 05:07:02

IgnorantGuru
Member
Registered: 2009-11-09
Posts: 640
Website

Re: Bash Question [Answered]

If I understood you correctly, I think eval is you answer.  Have a look at this.

#!/bin/bash

progressref () {  #$1=name of variable
    y=\$"$1"                        # Name of variable
    localvar=`eval "expr \"$y\" "`    # localvar set to contents of named variable
    result="xxx"$localvar
    eval "$1=\"$result\""            # named variable set to result
}

testvar="test"
progressref testvar
echo $testvar

Offline

#3 2009-11-24 05:16:46

Mardoct
Member
Registered: 2009-08-17
Posts: 208

Re: Bash Question [Answered]

Ahh, worked perfectly. Thanks.


The human being created civilization not because of willingness but of a need to be assimilated into higher orders of structure and meaning.

Offline

#4 2009-11-24 14:12:06

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: Bash Question [Answered]

@IgnorantGuru:
I think you just lived up to the 2nd part of your handle!!
That's cool!

Offline

#5 2009-11-24 17:32:31

IgnorantGuru
Member
Registered: 2009-11-09
Posts: 640
Website

Re: Bash Question [Answered]

@perbh
Those rare occassions are always nice, especially with so much of the first part going on!

It is a cool Bash thing almost like pointers - handy for setting a whole bunch of named variables from command line arguments too, without having explicit code for each one.

Offline

Board footer

Powered by FluxBB