Unix:源代码和导出文件有什么区别? [英] Unix: What is the difference between source and export?

查看:91
本文介绍了Unix:源代码和导出文件有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个shell脚本,以读取具有key = value对的文件并将这些变量设置为环境变量.但是我有一个疑问,如果我执行source file.txt会将该文件中定义的变量设置为环境变量,还是应该逐行读取文件并使用导出命令进行设置?

I am writing a shell script, to read a file which has key=value pair and set those variables as environment variables. But I have a doubt, if I do source file.txt will that set the variables defined in that file as environment variable or I should read the file line by line and set it using export command ?

在这种情况下,源命令是否不同于导出?

Is source command in this case different than export?

推荐答案

source文件时,将设置分配,但是除非设置了allexport选项,否则不会导出变量.如果要导出所有变量,则使用allexportsource文件比读取文件并显式使用export要简单得多.换句话说,您应该执行以下操作:

When you source the file, the assignments will be set but the variables are not exported unless the allexport option has been set. If you want all the variables to be exported, it is much simpler to use allexport and source the file than it is to read the file and use export explicitly. In other words, you should do:

set -a
. file.txt

(我更喜欢.,因为它比source更可移植,但是sourcebash中可以正常工作.)

(I prefer . because it is more portable than source, but source works just fine in bash.)

请注意,导出变量不会使其成为环境变量.只是使其成为任何子Shell中的环境变量.

Note that exporting a variable does not make it an environment variable. It just makes it an environment variable in any subshell.

这篇关于Unix:源代码和导出文件有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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