在bash脚本设置变量 [英] Set variables in bash script

查看:169
本文介绍了在bash脚本设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

test.sh包括:

test.sh contains:

A=$1
B=$2

我设置test.sh和chmod 777

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"

结果:

"FirstVar:  SecondVar: "

我做了什么错了?

What did I do wrong?

推荐答案

当你运行你的脚本是这样的:

When you run your script like this:

./test.sh whatever

猛砸在你的脚本运行的shell其中除$ P $点命令的另一个实例。如果你想在当前shell设置变量,你应该使用命令,或点()。在这种情况下,在脚本中的命令将直接在当前shell执行。

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

或只是

. test.sh

这篇关于在bash脚本设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆