在本指南中,我们将了解 Linux 中的导出命令。 Export 是 Bash shell 的内置命令。它用于标记要传递给子进程的变量和函数。基本上,变量将包含在子进程环境中,而不会影响其他环境。为了更清楚地了解我们正在讨论的内容,让我们深入研究一下导出命令示例。
如果没有任何参数,该命令将生成或显示所有导出的变量。以下是预期输出的示例。
$ export
Sample output
如果您想查看当前 shell 上所有导出的变量,请使用-p
如示例所示的标志
$ export -p
Sample output
假设您有一个函数并且希望导出它,您该如何进行呢?在这种情况下,-f
使用标志。在此示例中,我们导出函数name ()
。首先,调用函数
$ name () { echo "Hello world"; }
然后使用导出它-f
flag
$ export -f name
接下来,调用 bash shell
$ bash
最后调用函数
$ name
Output
Hello World
You can also assign a value before exporting a function as shown
$ export name[=value]
例如,您可以在导出变量之前定义它,如下所示
$ student=Divya
在上面的示例中,变量“student”已被赋值为“Divya”以导出变量 run
$ export students
您可以使用printenv
命令验证变量的内容,如图所示
$ printenv students
Check the output below of the commands we have just executed Output The above can be achieved in 2 simple steps by declaring and exporting the variable in one line as shown
$ export student=Divya
显示变量运行
$ printenv student
Output This concludes our tutorial about export command. Go ahead and give it a try and see the magic! Your feedback is most welcome.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)