Set variables in bash script [duplicate]

☆樱花仙子☆ 提交于 2019-12-13 22:04:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!