如何用破折号创建shell变量? [英] How to create shell variable with dashes?

查看:80
本文介绍了如何用破折号创建shell变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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).

https://unix.stackexchange.com/questions/23659/can-shell-variable-name-include-a-hyphen-or-dash

这篇关于如何用破折号创建shell变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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