Linux 如何从bash脚本源文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19621394/
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-07 01:11:51  来源:igfitidea点击:

How to source file from bash script

linuxbashshellenvironment-variables

提问by franki3xe

I'm trying to source a file with an environment variable from my bash script, but it doesn't work.

我试图从我的 bash 脚本中获取一个带有环境变量的文件,但它不起作用。

This is the content of my script (test.sh), which is located in ~/scripts/test.sh.

这是我的脚本 ( test.sh)的内容,它位于~/scripts/test.sh.

#!/bin/bash
FILE_NAME=/tmp/source_file
touch $FILE_NAME
echo "export TEST=\"test\"" > $FILE_NAME
source $FILE_NAME

Then I use aliasin my ~/.bashrc.

然后我aliasmy ~/.bashrc.

alias testScript=~/scripts/test.sh

But when I use my script testScript, it didn't set the environment variable.

但是当我使用我的脚本时testScript,它没有设置环境变量。

采纳答案by Paul

Environment variables only flow downstream in the process tree.

环境变量仅在流程树中向下游流动。

When you type testScriptto a bash process, it creates a child process and execs /bin/bashor whatever is set by #!

当您键入testScriptbash 进程时,它会创建一个子进程和 execs/bin/bash或由#!

Any environment variables set there remain only with the child process. Export causes the variables to be copied to additional grandchildren (children of that child) that might be spawned from that child.

在那里设置的任何环境变量只保留在子进程中。导出导致变量被复制到可能从该子项产生的其他孙子项(该子项的子项)。

Nothing can copy back to a parent. You need to use source instead of running the file. See Jonathan's answer.

没有什么可以复制回父级。您需要使用 source 而不是运行文件。请参阅乔纳森的回答。

You could try editing the files ~/.bashrcor ~/.loginto set enviornment variables you need frequently.

您可以尝试编辑文件 ~/.bashrc~/.login设置您经常需要的环境变量。

See also https://superuser.com/q/153371and https://superuser.com/questions/18988/difference-between-a-b-and-export-a-b-in-bashfor more explanation of exportin bash.

另请参阅https://superuser.com/q/153371https://superuser.com/questions/18988/difference-between-ab-and-export-ab-in-bash了解更多关于exportin 的解释bash

回答by Jonathan Leffler

You need to use:

您需要使用:

alias testScript=". ~/scripts/test.sh"

to source the file. Or you can use sourcein place of ., but I don't much like C shells so I don't use C shell notations such as source.

源文件。或者您可以使用source代替.,但我不太喜欢 C shell,所以我不使用 C shell 符号,例如source.

回答by Nikksno

None of the other methods worked for me [source /path/to/filevs . ./path/to/file, alias, etc...], until, thanks to this tutorialI found that using the:

其他方法都不适合我 [ source /path/to/filevs . ./path/to/file,别名等...],直到,感谢本教程,我发现使用:

#!/usr/bin/env bashshebang

#!/usr/bin/env bash舍邦

instead of the simpler #!/usr/bin/envone lets arguments pass on to the interpreter, which I think is the key here – see this documentfor more info.

而不是更简单的#!/usr/bin/env让参数传递给解释器,我认为这是这里的关键 - 请参阅此文档以获取更多信息。

In any event, if source commands in any form aren't working for you, try checking your shebang, that might be the problem :)

无论如何,如果任何形式的源命令都不适合您,请尝试检查您的shebang,这可能是问题所在:)