如何从命令行覆盖bash脚本中的变量?我知道如何传递变量,但我只是想要像./myscript.sh -Dvar = val这样的东西
您需要对要覆盖的变量使用参数扩展:
$ cat override.sh #!/bin/bash : ${var1:=foo} # var1 will take on the value "foo" if not overridden var2=${var2:-foo} # same thing but more typing echo "var1 is $var1 | var2 is $var2"
没有覆盖值
$ ./override.sh var1 is foo | var2 is foo
覆盖值
$ var1=bar var2=baz ./override.sh var1 is bar | var2 is baz