简体   繁体   English

如何在bash_profile文件中添加export语句?

[英]How to add export statement in a bash_profile file?

I'm trying to learn that if I have to add export statement to set a variable in a bash_profile file . 我试图了解如果我必须添加export语句来设置bash_profile文件中的变量。 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 例如,如果我必须添加导出AX ='name',那么我应该只在文件末尾写入它还是我还需要编写其他任何内容

Simply write export AS='name' anywhere in your ~/.bash_profile file: 只需在~/.bash_profile文件中export AS='name'任何位置写出export AS='name'

# 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。

There are 2 scenarios: 有两种情况:

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

Typically, variables are declared and defined in one place and exported in another: 通常,变量在一个地方声明和定义,并在另一个地方导出:

AX='name'
export AX

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

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