我可以将变量从 bash 脚本导出到环境中而不使用它吗?

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

Can I export a variable to the environment from a bash script without sourcing it?

bashshellunixenvironment-variables

提问by tarrsalah

Suppose that I have this script

假设我有这个脚本

export.bash:

出口.bash

#! /usr/bin/env bash
export VAR="HELLO, VARIABLE"

When I execute the script and try to access to the $VAR, I get no value!

当我执行脚本并尝试访问 时$VAR,我没有任何价值!

echo $VAR

Is there any way to access to the $VARby just executing export.bashwithout sourcing it?

有没有办法$VAR通过只执行export.bash而不采购它来访问它?

回答by Keith Thompson

Is there any way to access to the $VARby just executing export.bashwithout sourcing it ?

有什么方法可以$VAR通过只执行export.bash而不采购它来访问它吗?

Quick answer: No.

快速回答:没有。

But there are several possible workarounds.

但是有几种可能的解决方法。

The most obvious one, which you've already mentioned, is to use sourceor .to execute the script in the context of the calling shell:

您已经提到的最明显的一个是在调用 shell 的上下文中使用source.执行脚本:

$ cat set-vars1.sh 
export FOO=BAR
$ . set-vars1.sh 
$ echo $FOO
BAR

Another way is to have the script, rather than setting an environment variable, print commands that will set the environment variable:

另一种方法是让脚本,而不是设置环境变量,打印将设置环境变量的命令:

$ cat set-vars2.sh
#!/bin/bash
echo export FOO=BAR
$ eval "$(./set-vars2.sh)"
$ echo "$FOO"
BAR

A third approach is to have a script that sets your environment variable(s) internally and then invokes a specified command with that environment:

第三种方法是使用脚本在内部设置您的环境变量,然后使用该环境调用指定的命令:

$ cat set-vars3.sh
#!/bin/bash
export FOO=BAR
exec "$@"
$ ./set-vars3.sh printenv | grep FOO
FOO=BAR

This last approach can be quite useful, though it's inconvenient for interactive use since it doesn't give you the settings in your current shell (with all the other settings and history you've built up).

最后一种方法非常有用,尽管它不便于交互使用,因为它没有为您提供当前 shell 中的设置(以及您建立的所有其他设置和历史记录)。

回答by V H

In order to export out the VAR variable first the most logical and seems working way is to source the variable:

为了首先导出 VAR 变量,最合乎逻辑且似乎可行的方法是获取变量:

. ./export.bash

or

或者

source ./export.bash

Now when echoing from main shell it works

现在,当从主 shell 回显时,它可以工作

 echo $VAR
HELLO, VARABLE

We will now reset VAR

我们现在将重置 VAR

export VAR=""
echo $VAR

Now we will execute a script to source the variable then unset it :

现在我们将执行一个脚本来获取变量然后取消设置它:

./test-export.sh 
HELLO, VARABLE
--
.

the code: cat test-export.sh

代码:cat test-export.sh

    #!/bin/bash
    # Source env variable
    source ./export.bash

    # echo out the variable in test script
    echo $VAR

    # unset the variable 
    unset VAR
    # echo a few dotted lines
    echo "---"
    # now return VAR which is blank
    echo $VAR

Here is one way

这是一种方法

PLEASE NOTE: The exports are limited to the script that execute the exports in your main console - so as far as a cron job I would add it like the console like below... for the command part still questionable: here is how you would run in from your shell:

请注意:导出仅限于在您的主控制台中执行导出的脚本 - 所以就 cron 作业而言,我会像下面这样的控制台添加它......对于命令部分仍然有问题:这是您的方式从你的 shell 运行:

On your command prompt (so long as the export.bash has multiple echo values)

在您的命令提示符下(只要 export.bash 有多个回显值)

IFS=$'\n'; for entries in $(./export.bash); do  export $entries;  done; ./v1.sh 
HELLO THERE
HI THERE

cat v1.sh

猫 v1.sh

#!/bin/bash
echo $VAR
echo $VAR1

Now so long as this is for your usage - you could make the variables available for your scripts at any time by doing a bash alias like this:

现在只要这是供您使用的 - 您可以随时通过执行如下 bash 别名来使变量可用于您的脚本:

myvars ./v1.sh
HELLO THERE
HI THERE

echo $VAR

.

add this to your .bashrc

将此添加到您的 .bashrc

function myvars() { 
    IFS=$'\n'; 
    for entries in $(./export.bash); do  export $entries;  done; 

    "$@"; 

    for entries in $(./export.bash); do variable=$(echo $entries|awk -F"=" '{print }'); unset $variable;
done

}

source your bashrc file and you can do like above any time ...

源您的 bashrc 文件,您可以随时执行上述操作...

Anyhow back to the rest of it..

无论如何回到它的其余部分..

This has made it available globally then executed the script..

这使它在全局可用,然后执行脚本..

simply echo it out then run export on the echo !

只需将其回显,然后在 echo 上运行 export !

cat export.bash

猫导出.bash

#!/bin/bash
echo "VAR=HELLO THERE"

Now within script or your console run:

现在在脚本或您的控制台中运行:

 export "$(./export.bash)"

Try:

尝试:

echo $VAR
HELLO THERE

Multiple values so long as you know what you are expecting in another script using above method:

只要您知道使用上述方法在另一个脚本中期望什么,就可以使用多个值:

cat export.bash

猫导出.bash

#!/bin/bash
echo "VAR=HELLO THERE"
echo "VAR1=HI THERE"

cat test-export.sh

cat test-export.sh

#!/bin/bash

IFS=$'\n'
for entries in $(./export.bash); do
   export $entries
done

echo "round 1"
echo $VAR
echo $VAR1

for entries in $(./export.bash); do
     variable=$(echo $entries|awk -F"=" '{print }');
     unset $variable
done

echo "round 2"
echo $VAR
echo $VAR1

Now the results

现在的结果

 ./test-export.sh 
round 1
HELLO THERE
HI THERE
round 2


.

and the final final update to auto assign read the VARIABLES:

自动分配的最终更新读取变量:

./test-export.sh 
Round 0 - Export out then find variable name - 
Set current variable to the variable exported then echo its value
$VAR has value of HELLO THERE
$VAR1 has value of HI THERE
round 1 - we know what was exported and we will echo out known variables
HELLO THERE
HI THERE
Round 2 - We will just return the variable names and unset them 
round 3 - Now we get nothing back

The script: cat test-export.sh

脚本:cat test-export.sh

#!/bin/bash

IFS=$'\n'
echo "Round 0 - Export out then find variable name - "
echo "Set current variable to the variable exported then echo its value"
for entries in $(./export.bash); do
 variable=$(echo $entries|awk -F"=" '{print }');
 export $entries
 eval current_variable=$$variable
 echo "$$variable has value of $current_variable"
done


echo "round 1 - we know what was exported and we will echo out known variables"
echo $VAR
echo $VAR1

echo "Round 2 - We will just return the variable names and unset them "
for entries in $(./export.bash); do
 variable=$(echo $entries|awk -F"=" '{print }');
 unset $variable
done

echo "round 3 - Now we get nothing back"
echo $VAR
echo $VAR1

回答by mdornfe1

Execute

执行

set -o allexport

Any variables you source from a file after this will be exported in your shell.

在此之后您从文件中获取的任何变量都将导出到您的 shell 中。

source conf-file

When you're done execute. This will disable allexport mode.

完成后执行。这将禁用 allexport 模式。

set +o allexport

回答by Van

Found an interesting and neat way to export environment variables from a file:

找到了一种从文件中导出环境变量的有趣且简洁的方法:

in env.vars:

env.vars

foo=test

test script:

测试脚本:

eval `cat env.vars`
echo $foo         # => test
sh -c 'echo $foo' # => 

export eval `cat env.vars`
echo $foo         # => test
sh -c 'echo $foo' # => test

# a better one
export `cat env.vars`
echo $foo         # => test
sh -c 'echo $foo' # => test

回答by Gonmator

Another workaround that, depends on the case, it could be useful: creating another bash that inherites the exported variable. It is a particular case of @Keith Thompson answer, will all of those drawbacks.

另一种解决方法取决于具体情况,它可能很有用:创建另一个继承导出变量的 bash。这是@Keith Thompson 回答的一个特例,所有这些缺点都会出现。

export.bash:

导出.bash:

# !/bin/bash
export VAR="HELLO, VARIABLE"
bash

Now:

现在:

./export.bash
echo $VAR

回答by zhi.yang

Maybe you can write a function in ~/.zshrc, ~/.bashrc .

也许你可以在 ~/.zshrc, ~/.bashrc 中编写一个函数。

# set my env
[ -s ~/.env ] && export MYENV=`cat ~/.env`
function myenv() { [[ -s ~/.env ]] && echo $argv > ~/.env && export MYENV=$argv }

Beacause of use variable outside, you can avoid write script file.

由于在外部使用变量,可以避免编写脚本文件。

回答by Malcomar

The answer is no, but for me I did the following

答案是否定的,但对我来说我做了以下

the script: myExport

脚本:myExport

#! \bin\bash
export 

an alias in my .bashrc

我的 .bashrc 中的别名

alias myExport='source myExport' 

Still you source it, but maybe in this way it is more useable and it is interesting for someone else.

您仍然可以获取它,但也许通过这种方式它更有用,并且对其他人很有趣。