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

bashshell

提问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 = $1actually does the following:

该命令set var1 = $1实际上执行以下操作:

  1. Sets the value of $1to "var1"
  2. Sets the value of $2to "="
  3. Sets the value of $3to the original first parameter $1.
  1. 将 的值设置$1为“var1”
  2. 将 的值设置$2为“=”
  3. 将 的值设置为$3原始第一个参数$1