Reputation: 23
I have a bash script that will eventually call another bash script. Each script must be run with "source". For simplicity's sake, I've summarized the problem points below
script1.sh:
source script2.sh
script2.sh:
export someVar=something
Run everything with:
source script1.sh arg1 arg2
The issue is when script2.sh is run from script1.sh, the arguments are also copied, so script2.sh is actually run as:
source script2.sh arg1 arg2
script2.sh fails because those arguments are provided. Is there any way that I can run script2 from script1 without passing those args? Running script2 without the source command is not an option, unless there is another way for it to be run and let the variables persist. I also can not modify script2 in any way.
Upvotes: 2
Views: 117
Reputation: 123400
You can clear the positional parameters using set --
when you're done with them:
script1.sh:
echo "Number of parameters before: $#"
set --
echo "Number of parameters after : $#"
source script2.sh
script2.sh:
echo "script2.sh received $# parameters"
Now script1.sh foo bar
will print
Number of parameters before: 2
Number of parameters after : 0
script2.sh received 0 parameters
Upvotes: 2