在Linux环境中,我想用破折号创建一个变量名.这是可能的,因为我可以在jenkins中设置一个类似的名称,为此 env 提供输出(在其他行中):
In a Linux environment I want to create a variable name with dashes. This is possible as I can set a name like that in jenkins, for which env gives the output (amongst other lines):
variable-with-dashes=test但是如何直接在shell上执行呢?做
But how can do that directly on the shell? Doing
export variable-with-dashes=test出现错误
-bash: export: `variable-with-dashes=test': not a valid identifier在两种情况下,外壳似乎都是/bin/bash .
In both cases the shell seems to be /bin/bash.
推荐答案我从未遇到过允许使用-变量名的Bourne风格的shell.仅支持ASCII字母(无论哪种情况),_和数字,并且第一个字符不能为数字.
I've never met a Bourne-style shell that allowed - in a variable name. Only ASCII letters (of either case), _ and digits are supported, and the first character must not be a digit.
如果您的程序需要的环境变量与shell限制不匹配,请与env程序一起启动它.
If you have a program that requires an environment variable that doesn't match the shell restrictions, launch it with the env program.
env'strange-name = some value'myprogram
env 'strange-name=some value' myprogram
请注意,某些shell(例如,现代破折号,mksh,zsh)会从环境中删除其名称不喜欢的变量.(Shellshock使人们对环境变量名称更加谨慎,因此限制可能会随着时间的流逝而变得更加严格,而不是更加宽松.)因此,如果您需要将名称中包含特殊字符的变量传递给程序,请直接将其传递,之间没有外壳(env'strange-name = some value'sh -c'...; myprogram'可能有效,也可能无效).
Note that some shells (e.g. modern dash, mksh, zsh) remove variables whose name they don't like from the environment. (Shellshock has caused people to be more cautious about environment variable names, so restrictions are likely to become tighter over time, not more permissive.) So if you need to pass a variable whose name contains special character to a program, pass it directly, without a shell in between (env 'strange-name=some value' sh -c'…; myprogram' may or may not work).
unix.stackexchange/questions/23659/can-shell-variable-name-include-a-hyphen-or-dash
更多推荐
如何用破折号创建shell变量?
发布评论