linux - 在 bash 脚本中设置变量

标签 linux bash shell command-line echo

test.sh 包含:

A=$1
B=$2

我将 test.sh 设置为 chmod 777

我使用 2 个参数启动脚本:

./test.sh first last

然后我通过输入进行测试:

echo "FirstVar: $A SecondVar: $B"

结果:

"FirstVar:  SecondVar: "

我做错了什么?

最佳答案

当您像这样运行脚本时:

./test.sh whatever

Bash 运行另一个 shell 实例来解释脚本中的命令。如果要在当前 shell 中设置变量,应使用 source 命令或点 (.)。在这种情况下,脚本中的命令将直接在当前 shell 中执行。

source test.sh

或者只是

. test.sh

关于linux - 在 bash 脚本中设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35586479/

相关文章:

c++ - 在 boost::asio::io_service 下获取 boost::process::child 的退出代码?

linux - 增加ssh超时

c - 在 C 中等待用户输入?

linux - Shell 脚本仅在通过 ssh 使用时启动应用程序

linux - Bash 脚本 - 删除重复项

linux - 带参数 "\"的 bash shell 脚本

linux - 删除格式的所有两行的命令

php - UTF-8贯穿始终

arrays - 如何使用 Bash 数组作为命令的输入?

linux - 将 find -execdir 与 grep 结合使用