在bash中导出环境变量时何时使用方括号? [英] When to use brackets when exporting environment variables in bash?

查看:49
本文介绍了在bash中导出环境变量时何时使用方括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试弄清楚bash环境变量中括号的作用是什么.例如,在下面的实际代码示例中,为什么某些定义在 PATH 周围使用 {} ,例如, export ... =.../$ {PATH} .还要注意,其中一些定义是不同的:一些使用 {$ ECLIPSE_DIR} 和方括号内的 $ .有些将 $ {PATH} $ 放在方括号之外,有些则完全省略了方括号.这段代码通常可以正常工作,尽管有时会显示出底部所示的错误(它们似乎是暂时的),而且我不确定为什么这种错误有时只出现而不是其他.

I've been trying to figure out what is the purpose of brackets in the bash environment variables. For example, in the below actual example of code, why are some of the definitions using a {} around the PATH, for example, export ...=.../${PATH}. Note also that some of the definitions are different: some use {$ECLIPSE_DIR} with the $ within the brackets; some use ${PATH} with the $ outside of the brackets, and some omit brackets altogether. 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.

关于包括bash环境变量的方法的常见做法是什么?何时应使用方括号?将 $ 放在方括号内和方括号之间有什么区别?另外,为什么有些行在变量名之前有一个" export ",而有些却没有?这里有什么区别?

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? Also, why do some lines have an "export" before the variable name, and some do not? 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

看着您所出现的错误, {$ ECLIPSE_DIR} 将变量展开,然后用直括号将其括起来.我认为解决方案应该将其更改为 $ {ECLIPSE_DIR}

Looking at that error you presented, {$ECLIPSE_DIR} gets the variable expanded and then surrounded with literal open and close braces. I think the solution should be changing it to ${ECLIPSE_DIR}

为回答 export 问题,export用于使调用此脚本的shell可以访问变量.脚本完成后,脚本中设置的任何变量都不存在,除非将其导出.因此,如果您希望在运行脚本后更改 PATH ,则必须在脚本结束之前调用 export PATH .

In response to the export question, export is used to make a variable accessible to the shell that called this script. 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.

这篇关于在bash中导出环境变量时何时使用方括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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