Linux 如何在 bash_profile 文件中添加导出语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14524590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:48:17  来源:igfitidea点击:

How to add export statement in a bash_profile file?

linuxbash.bash-profile

提问by Lost

I'm trying to learn that if I have to add export statement to set a variable in a bash_profile file . How would I do that ? For example if I have to add export AX = 'name' then should I simply write it at the end of file or do I need to write anything else as well

我正在尝试了解是否必须添加 export 语句以在 bash_profile 文件中设置变量。我该怎么做?例如,如果我必须添加 export AX = 'name' 那么我应该简单地将它写在文件的末尾还是我还需要写其他任何东西

采纳答案by Chris Seymour

Simply write export AS='name'anywhere in your ~/.bash_profilefile:

只需export AS='name'~/.bash_profile文件中的任何位置写入:

# Append to the end of the file
$ echo "export AS='name'" >> ~/.bash_profile

# Update shell 
$ source ~/.bash_profile

This first command adds the line you want to the file (or just use a text editor)the second updates the shells with the new variable.

第一个命令将您想要的行添加到文件中(或仅使用文本编辑器),第二个命令使用新变量更新 shell。

回答by Lucas

Typically, variables are declared and defined in one place and exported in another:

通常,变量在一个地方声明和定义并在另一个地方导出:

AX='name'
export AX

回答by Hemant Kumar

There are 2 scenarios:

有2种情况:

1. Exporting an independent variable

1. 导出自变量

For example if you want to export variable "AX" independently then use:

例如,如果您想独立导出变量“AX”,则使用:

AX = 'name'
export AX

2. Exporting an independent variable followed by appending it to some existing variable

2. 导出自变量,然后将其附加到某个现有变量

For example if you want to export variable "AX" independently followed by appending it to the class path then use:

例如,如果要独立导出变量“AX”,然后将其附加到类路径,请使用:

AX = 'name'
export AX
PATH=$PATH:AX
export PATH