简体   繁体   English

bash中导出环境变量时什么时候用括号?

[英]When to use brackets when exporting environment variables in bash?

I've been trying to figure out what is the purpose of brackets in the bash environment variables.我一直试图弄清楚 bash 环境变量中括号的用途是什么。 For example, in the below actual example of code, why are some of the definitions using a {} around the PATH , for example, export...=.../${PATH} .例如,在下面的实际代码示例中,为什么某些定义在PATH周围使用{} ,例如export...=.../${PATH} Note also that some of the definitions are different: some use {$ECLIPSE_DIR} with the $ within the brackets;另请注意,有些定义是不同的:有些使用{$ECLIPSE_DIR}和括号内的$ some use ${PATH} with the $ outside of the brackets, and some omit brackets altogether.有些使用${PATH}和括号外的$ ,有些则完全省略括号。 This code generally works, although sometimes errors like the one shown at the bottom are shown (they appear to be transient), and I'm not sure why such errors only show up sometimes and not others.这段代码通常可以工作,尽管有时会显示底部显示的错误(它们似乎是暂时的),我不确定为什么这些错误有时只出现而不出现其他错误。

What are the common practices concerning ways to include bash environment variables, when should brackets be used, and what is the difference between putting the $ inside and outside of brackets?关于包含 bash 环境变量的方法的常见做法是什么,什么时候应该使用括号,将$放在括号内和括号外有什么区别? Also, why do some lines have an " export " before the variable name, and some do not?另外,为什么有些行在变量名前有一个“ export ”,而有些则没有? What is the difference here?这里有什么区别?

# ECLIPSE
ECLIPSE_DIR=$HOME/eclipse
PATH=${PATH}:{$ECLIPSE_DIR}

# ANT
ANT_HOME=/usr/bin/ant
PATH=${ANT_HOME}/bin:${PATH}
export ANT_HOME PATH

# GRADLE
export GRADLE_HOME=/usr/local/gradle
export PATH=$GRADLE_HOME/bin:$PATH</code>


-bash: export: `/usr/bin/ant/bin:/usr/local/bin:{/Users/me/eclipse}:/usr/bin/scala-2.9.0.1/bin:/usr/local/mysql/bin:/usr/local/bin:{/Users/me/eclipse}': not a valid identifier

The braces are usually used for clarity, but a practical use is breaking up text from variable names.大括号通常用于清楚起见,但实际用途是将文本与变量名分开。 Say I had the following:说我有以下内容:

$ word="ello"
$ echo "h$word"
hello
$ echo "y$wordw" # bash tries to find the var wordw, and replaces with a blank
y
$ echo "y${word}w"
yellow

Variable names are automatically separated by most punctuation (notably. or /).大多数标点符号(特别是 . 或 /)会自动分隔变量名。

echo "$word/$word.$word"
ello/ello.ello

Looking at that error you presented, {$ECLIPSE_DIR} gets the variable expanded and then surrounded with literal open and close braces.查看您提出的错误, {$ECLIPSE_DIR}将变量展开,然后用文字打开和关闭大括号括起来。 I think the solution should be changing it to ${ECLIPSE_DIR}我认为解决方案应该将其更改为${ECLIPSE_DIR}

In response to the export question, export is used to make a variable accessible to the shell that called this script.针对export问题,导出用于使调用此脚本的 shell 可以访问变量。 Any variable set up in a script does not exist once the script is finished unless it is exported.一旦脚本完成,脚本中设置的任何变量都不存在,除非它被导出。 Hence, if you want your PATH to change after running that script, export PATH will have to be called before the script is over.因此,如果您希望在运行该脚本后更改PATH ,则必须在脚本结束之前调用export PATH

Braces are used with bash variables to disambiguate between variables.大括号与 bash 变量一起使用以消除变量之间的歧义。 For example, consider this:例如,考虑一下:

VAR=this
echo $VAR_and_that
echo ${VAR}_and_that

The first echo prints nothing, since bash thinks you are trying to echo out the var this_and_that which of course doesn't exist.第一个echo什么也不打印,因为bash认为您正在尝试echo显 var this_and_that当然不存在。 The second echo doesn't have this problem and outputs this_and_that , since bash knows to expand out the VAR variable due to the braces.第二个echo显没有这个问题并输出this_and_that ,因为 bash 知道由于大括号而扩展了 VAR 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM