Bash is a shell and a simple programming shell too. There are a lot of variables built-in or externally specified. If a variable is specified externally making this variable system-wide available is a problem. The export command makes variables available system-wide by propagating to the subshells. An exported variable would not be available to the parent processes. This means previously spawned or created process will not get exported variable. Syntax of export is like below there is two way to export some variable.
Bash也是一个shell和一个简单的编程shell。 有很多内置或外部指定的变量。 如果在外部指定了变量,则使该变量在系统范围内可用是一个问题。 通过传播到子shell,export命令使变量在系统范围内可用。 导出的变量对父进程将不可用。 这意味着先前生成或创建的进程将不会获得导出的变量。 导出的语法如下所示,有两种导出某些变量的方法。
直接出口 (Directly Export)
We can export the newly created variable with the following syntax by using export
keyword.
我们可以使用export
关键字以以下语法导出新创建的变量。
export VAR
In this case, we export all readily defined variables to the subshells.
在这种情况下,我们将所有容易定义的变量导出到子shell。
$ myfw=10.0.0.1
$ export myfw
定义后导出(Export After Defining)
We can use the following syntax in order to export all readily defined bash variables.
我们可以使用以下语法来导出所有易于定义的bash变量。
export VAR=VALUE
In this model, the variable is just defined before exported. After variable definition variable exported to the system-wide.
在此模型中,变量仅在导出之前定义。 变量定义后将变量导出到系统范围内。
$ export httpserver=192.168.1.10
列出导出的变量(List Exported Variables)
It can be listed exported variables with the -p
parameter.
可以使用-p
列出导出的变量 参数。
$ export -p
We can see from the output that there are a lot of exported variables to the bash shell. Some of them are HOME
, LANG
,LOGNAME
etc.
从输出中我们可以看到,有很多导出到bash shell的变量。 其中一些是HOME
, LANG
, LOGNAME
等。
翻译自: https://www.poftut.com/linux-bash-shell-export-tutorial-with-examples/