问题
test.sh contains:
A=$1
B=$2
I set test.sh to chmod 777
I started the script with 2 parameters:
./test.sh first last
Then I tested it by typing:
echo "FirstVar: $A SecondVar: $B"
Result:
"FirstVar: SecondVar: "
What did I do wrong?
回答1:
When you run your script like this:
./test.sh whatever
Bash runs another instance of shell which interprets command in your script. If you want to set variables in the current shell, you should use source
command, or dot (.
). In this case the commands in the script will be executed in the current shell directly.
source test.sh
or just
. test.sh
来源:https://stackoverflow.com/questions/35586479/set-variables-in-bash-script