如何用破折号创建shell变量?

编程入门 行业动态 更新时间:2024-10-24 03:22:13
本文介绍了如何用破折号创建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).

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

更多推荐

如何用破折号创建shell变量?

本文发布于:2023-07-20 17:49:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169699.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:破折号   变量   如何用   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!