如何从bash脚本源文件

时间:2021-02-25 09:11:27

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.

然后在~/.bashrc中使用别名。

alias testScript=~/scripts/test.sh

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

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

3 个解决方案

#1


9  

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 #!

当您向bash进程输入testScript时,它将创建一个子进程和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或(~ /。登录设置您需要经常使用的环境变量。

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

参见https://superuser.com/q/153371和https://superuser.com/questions/18988/difference- a- band -export-a-b-in-bash来了解更多关于导出的解释。

#2


9  

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。

#3


3  

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

其他的方法对我都不起作用[源/路径/到/文件vs ./路径/到/文件,别名,等等……],直到,感谢本教程,我发现使用:

#!/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 document for more info.

而不是简单的#!/usr/bin/env 1让参数传递给解释器,我认为这是这里的关键——更多信息请参见本文档。

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

在任何情况下,如果任何形式的源命令对您不起作用,请尝试检查您的shebang,这可能是问题所在:)

#1


9  

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 #!

当您向bash进程输入testScript时,它将创建一个子进程和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或(~ /。登录设置您需要经常使用的环境变量。

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

参见https://superuser.com/q/153371和https://superuser.com/questions/18988/difference- a- band -export-a-b-in-bash来了解更多关于导出的解释。

#2


9  

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。

#3


3  

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

其他的方法对我都不起作用[源/路径/到/文件vs ./路径/到/文件,别名,等等……],直到,感谢本教程,我发现使用:

#!/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 document for more info.

而不是简单的#!/usr/bin/env 1让参数传递给解释器,我认为这是这里的关键——更多信息请参见本文档。

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

在任何情况下,如果任何形式的源命令对您不起作用,请尝试检查您的shebang,这可能是问题所在:)