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
How to source file from bash script
提问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 alias
in my ~/.bashrc
.
然后我alias
在my ~/.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 testScript
to a bash process, it creates a child process and execs /bin/bash
or whatever is set by #!
当您键入testScript
bash 进程时,它会创建一个子进程和 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 ~/.bashrc
or ~/.login
to 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 export
in bash
.
另请参阅https://superuser.com/q/153371和https://superuser.com/questions/18988/difference-between-ab-and-export-ab-in-bash了解更多关于export
in 的解释bash
。
回答by Jonathan Leffler
You need to use:
您需要使用:
alias testScript=". ~/scripts/test.sh"
to source the file. Or you can use source
in 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/file
vs . ./path/to/file
, alias, etc...], until, thanks to this tutorialI found that using the:
其他方法都不适合我 [ source /path/to/file
vs . ./path/to/file
,别名等...],直到,感谢本教程,我发现使用:
#!/usr/bin/env bash
shebang
#!/usr/bin/env bash
舍邦
instead of the simpler #!/usr/bin/env
one 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,这可能是问题所在:)