Shell Variables and 'indirect reference'

I don't know how to ask this question, the concept and my english are fuzzy.
I need to set some variables in a function, passing them by name, I read this before ask (I'm not shure if I understood all), the variables cannot be set, only read http://www.tldp.org/LDP/abs/html/ivr.html

The concept is

Code:
function VarsByRef ()
{
    ${$1}=100
    ${$2}=200
    ${$3}="some other text"
}

a=1
b=2
c="some text"

echo "Before 'VarsByRef'"
echo "a=$a"
echo "b=$b"
echo "c=$c"

VarsByRef a b c

echo "After 'VarsByRef'"
echo "a=$a"
echo "b=$b"
echo "c=$c"

the result should be

Code:
Before 'VarsByRef'
a=1
b=2
c=some text
After 'VarsByRef'
a=100
b=200
c=some other text

Is there a way? Thank you all
 
Use eval. Here's an example:
Code:
a=new_var
echo $new_var # => empty, because $new_var is not set
eval "$a=bla"
echo $new_var # => bla
 
Ah, thank you tobik. In the while... my example above is not what I really mean, it must be modified as follow

Code:
function FuncWithLocalVars
{
    local a b c

    a=10
    b=20
    c="some local text"

    echo "Before 'VarsByRef'"
    echo "a=$a"
    echo "b=$b"
    echo "c=$c"

    VarsByRef a b c

    echo "After 'VarsByRef'"
    echo "a=$a"
    echo "b=$b"
    echo "c=$c"
}

function VarsByRef ()
{
  ${$1}=100
  ${$2}=200
  ${$3}="some other text"
}

a=1
b=2
c="some text"

echo "Before 'FuncWithLocalVars'"
echo "a=$a"
echo "b=$b"
echo "c=$c"

FuncWithLocalVars

echo "After 'FuncWithLocalVars'"
echo "a=$a"
echo "b=$b"
echo "c=$c"

and the expected output

Code:
Before 'FuncWithLocalVars'
a=1
b=2
c=some text
Before 'VarsByRef'
a=10
b=20
c=some local text
After 'VarsByRef'
a=100
b=200
c=some other text
Before 'FuncWithLocalVars'
a=1
b=2
c=some text

That is, the scope of a, b and c variables in function FuncWithLocalVars are local to that function, so calling a lot of time that funcion does not affects the global script variables with the same name.

However I still not verified how scope works in sh scripts, I solved this in another way, echoing directly on the screen what I whish to have in a variable (by reference), this modify a lot the structure of the script but it works anyway, and I must use the echo form echo -e "...\c".
 
Back
Top