Please notice that I'm talking about passing arguments by-ref to a script, not to a function. I already checked here and here and they discuss about passing arguments using functions.
What I want is to call a script passing an argument to it and having the argument becoming a variable in the shell with the value assigned inside the script (which is exactly what happens with the read RESPONSE
command, where RESPONSE becomes a variable in the current shell with the value assigned by the read
command).
I know that using functions like this (using local -n
):
#!/bin/bash
function boo() {
local -n ref=$1
ref="new"
}
SOME_VAR="old"
echo $SOME_VAR
boo SOME_VAR
echo $SOME_VAR
or like this (using eval
):
function booeval() {
ref=$1
eval $(echo $ref=\"new\")
}
SOME_VAR="old"
echo $SOME_VAR
booeval SOME_VAR
echo $SOME_VAR
work as expected. But local
(or declare
/typedef
) doesn't work outside of a function and eval
seems to run in a subshell that when the script is over the variable doesn't survive in the shell. I wanted to have something like this:
$ cat /tmp/test.sh
#!/bin/bash
ref=$1
eval $(echo "export $ref=\"new value\"")
$ /tmp/test.sh VAR
$ echo $VAR
$
Any ideas?