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

时间:2022-04-02 11:20:31

suppose that I have this script

假设我有这个脚本

export.bash :

export.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 $VAR by just executing export.bash without sourcing it ?

有没有办法通过执行export.bash来访问$ VAR而无需获取它?

7 个解决方案

#1


212  

Is there any way to access to the $VAR by just executing export.bash without sourcing it ?

有没有办法通过执行export.bash来访问$ VAR而无需获取它?

Quick answer: No.

快速回答:没有。

But there are several possible workarounds.

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

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

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

$ 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

Note that the $(...) will join the output into a single line. If you have more than one thing to set, you should add semicolons to the printed commands so they're still valid after being joined together.

请注意,$(...)会将输出连接到一行。如果您要设置多个内容,则应该在打印的命令中添加分号,这样它们在连接在一起后仍然有效。

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中的设置(包含您构建的所有其他设置和历史记录)。

#2


34  

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有多个echo值)

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 $1}'); 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 !

只需回显它然后在回声上运行导出!

cat export.bash

cat export.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

cat export.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 $1}');
     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:

并且自动分配的最终最终更新读取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 $1}');
 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 $1}');
 unset $variable
done

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

#3


10  

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

#4


2  

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:

export.bash:

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

Now:

现在:

./export.bash
echo $VAR

#5


2  

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

#6


1  

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.

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

#7


0  

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

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

the script: myExport

脚本:myExport

#! \bin\bash
export $1

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.

你仍然采购它,但也许以这种方式它更有用,它对其他人来说很有趣。

#1


212  

Is there any way to access to the $VAR by just executing export.bash without sourcing it ?

有没有办法通过执行export.bash来访问$ VAR而无需获取它?

Quick answer: No.

快速回答:没有。

But there are several possible workarounds.

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

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

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

$ 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

Note that the $(...) will join the output into a single line. If you have more than one thing to set, you should add semicolons to the printed commands so they're still valid after being joined together.

请注意,$(...)会将输出连接到一行。如果您要设置多个内容,则应该在打印的命令中添加分号,这样它们在连接在一起后仍然有效。

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中的设置(包含您构建的所有其他设置和历史记录)。

#2


34  

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有多个echo值)

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 $1}'); 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 !

只需回显它然后在回声上运行导出!

cat export.bash

cat export.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

cat export.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 $1}');
     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:

并且自动分配的最终最终更新读取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 $1}');
 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 $1}');
 unset $variable
done

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

#3


10  

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

#4


2  

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:

export.bash:

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

Now:

现在:

./export.bash
echo $VAR

#5


2  

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

#6


1  

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.

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

#7


0  

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

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

the script: myExport

脚本:myExport

#! \bin\bash
export $1

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.

你仍然采购它,但也许以这种方式它更有用,它对其他人来说很有趣。