I mean to have:
-
A bash script
scr.sh
that takes positional parameters#!/bin/bash echo "Params = $@" echo REMOTE_SERVER=${REMOTE_SERVER}
-
A bash function
f
defined in another scriptscr2.sh
#!/bin/bash f() { REMOTE_SERVER=s001 scr.sh "${@}" }
I would first
$ source scr2.sh
and then have f
available for calling at the command line, but not leaving a trace of what I did with REMOTE_SERVER
. For instance, I want
$ f par1 par2
par1 par2
s001
$ echo REMOTE_SERVER=${REMOTE_SERVER}
REMOTE_SERVER=
(actually, if REMOTE_SERVER
was set before using f
, I want it to keep that value). I couldn't attain this last objective. I always end up with REMOTE_SERVER
set.
I tried using multiline commands separated with a semicolon, enclosing commands inside f
with parenthesis, but it didn't work.
How can I do that?
Best Answer
If you want to set a variable only for a command, prefix the assignment to the command:
Alternately, use a subshell for the function (then variable assignments won't affect the parent shell). You can create a subshell by wrapping commands in
( ... )
(parentheses), or using parentheses instead of braces for the function body. For example, with:My output would be: