I am want to change a global variable (or at least append to it) using a function.
input="Hello"
example=input
func() {
declare -x $example="${input} World"
}
func
echo $input
The output of this would be "Hello" The original value. I would like it if the function were to able to change the original value. Is there alternative to accomplishing this. Please note I need to set example=input
and then perform on the operation on example (the variable).
BTW, if I used eval
instead the function will complain about World being a function or something.
You could use
eval
by redefining func() as the following:This allows the double-quotes to "survive" the first parsing (which expands the variables into their values, as needs to be done) so that eval starts parsing again with the string 'input="Hello World".
As for the use of
export
to do the job, if the variable input does not actually need to be exported, include its '-n' option:, and the variable remains a shell variable and does not get exported as an environment variable.
Did you try using export?
you should use
in your
func
see
http://www.gnu.org/software/bash/manual/bashref.html
Also note in MinGW, it seems that
declare
does not support the-g
option.