bash 从命令行参数设置变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20668556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:01:56 来源:igfitidea点击:
bash set variable from commandline argument
提问by user3116685
cat ./1.sh
猫./1.sh
#!/bin/bash
echo
set var1 =
echo var1 is $var1
kostas@elem:~/1$ argument1 var1 is
kostas@elem:~/1$ argument1 var1 是
How to set var1 from first commandline argument?
如何从第一个命令行参数设置 var1?
回答by chepner
The correct assignment is simply the following, with no spaces on either side of the equal sign:
正确的赋值如下,等号两边没有空格:
var1=
The command set var1 = $1
actually does the following:
该命令set var1 = $1
实际上执行以下操作:
- Sets the value of
$1
to "var1" - Sets the value of
$2
to "=" - Sets the value of
$3
to the original first parameter$1
.
- 将 的值设置
$1
为“var1” - 将 的值设置
$2
为“=” - 将 的值设置为
$3
原始第一个参数$1
。