gpt4 book ai didi

bash - shell变量的名称可以有空格吗?

转载 作者:行者123 更新时间:2023-12-05 09:12:36 25 4
gpt4 key购买 nike

我想导出一个带空格的变量名。是否可以在 shell export 命令中使用?

{'Truststore Filename': '/hom/truststore.jks', 'Truststore Password': 'pass'}
$ export 'Truststore Password'=clientpass
-bash: export: `Truststore Password=clientpass': not a valid identifier

但我想使用 export 命令导出变量,因为我们要导出的变量不止一个,名称中有空格。

但是,下面的方法是可行的。

$ env 'Truststore Password'=clientpass 'Truststore Filename'=/hom/truststore.jks python3 script.py

最佳答案

不,这是不可能的,因为a variable in bash cannot have a name that includes spaces :

name

A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier.

env,如您所见,按照不同的规则播放:

Environment variable names can be empty, and can contain any characters other than ‘=’ and ASCII NUL. However, it is wise to limit yourself to names that consist solely of underscores, digits, and ASCII letters, and that begin with a non-digit, as applications like the shell do not work well with other names.

(来源:info '(coreutils) env invocation';强调我的)

例子,here's another post with an answer about how much of a pain it is to grab the output of an environment variable with spaces

关于bash - shell变量的名称可以有空格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57747770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com