简体   繁体   English

在zsh上运行bash脚本不会获取环境变量

[英]running a bash script on zsh won't source the environment variables

I have the following bash script. 我有以下bash脚本。 It is actually an env variable file that contains some environment variables I want to set depending on an argument I pass in when I source it. 它实际上是一个环境变量文件,其中包含一些环境变量,我想根据我在获取源变量时传递的参数来设置这些环境变量。 Here is a MWE. 这是MWE。

#!/usr/bin/env bash

# CONSTANTS
PROD_STR='prod'
TEST_STR='test'

# Set the environment: 'prod' or 'test'
if [ "$1" == $PROD_STR ] || [ "$1" == $TEST_STR ]; then
    export DEV_ENV=$1
    echo "Found valid dev environment: "$DEV_ENV
else
    echo "No valid environment specified"
    echo "Usage: env_vars <env>"
    echo '       env - one of "prod" or "test"'
fi  

case $DEV_ENV in
    $PROD_STR)
        export MY_HOST='fooProd.net'
        export PORT='8000';;
    $TEST_STR)
        export MY_HOST='fooTest.net'
        export PORT='8100';;
esac

export API_USERNAME='foo'
export API_PASSWORD='mypwrd'

Now I wanted to source this file which I have named test_env_var.sh . 现在我想source这个文件,我有一个名为test_env_var.sh I tried. 我试过了。

  1. source test_env_var.sh prod It gives me the following test_env_var.sh:8: = not found source test_env_var.sh prod它给了我以下test_env_var.sh:8: = not found
  2. . ./test_env_var.sh prod . ./test_env_var.sh prod . . ./test_env_var.sh prod It still gives me: ./test_env_var:8: = not found . 它仍然给我: ./test_env_var:8: = not found

  3. ./test_env_var prod It now runs without errors and echoes (as it should) the output Found valid dev environment: prod . ./test_env_var prod现在可以正常运行,并且没有错误,并且回显(应该如此)输出Found valid dev environment: prod

So now after doing (3) I would expect to see my env variables set. 所以现在做完(3)之后,我希望看到我的env变量已设置。 But when I do 但是当我这样做

echo $API_USERNAME

It doesn't return anything. 它不返回任何东西。

Workaround 解决方法

I temporarily change my $SHELL into bash by typing bash on the prompt. 我通过在提示符下输入bash暂时将$ SHELL更改为bash。 Then I run the script as 然后我运行脚本为

. test_env_var prod

and now when I try to echo any env variable I have set (for example echo $API_USERNAME it returns me foo as it should ). 现在,当我尝试echo我设置的任何env variable (例如echo $API_USERNAME ,它应返回foo )。 Even if I change back to zsh by typing zsh the environment variables are still there. 即使我变回zsh键入zsh环境变量仍然存在。

Question: Why is this happening?. 问题:为什么会这样? Is there no way for me to run this bash script without changing the $SHELL to bash?. 如果不将$ SHELL更改为bash,我是否无法运行此bash脚本? Perhaps the syntax of the bash script needs to change so that it runs on zsh . 可能需要更改bash脚本的语法,以使其在zsh运行。 But that's not an option since this file is under version control. 但这不是一个选择,因为此文件受版本控制。

Some more context Both my zsh and bash are usr/local/bin/ so the shebang #!/usr/bin/env bash should take care of this, I would think. 更多上下文我的zshbash都是usr/local/bin/所以shebang #!/usr/bin/env bash应该解决这个问题。

Change 更改

if [ "$1" == $PROD_STR ] || [ "$1" == $TEST_STR ]; then

to

if [ "$1" = $PROD_STR ] || [ "$1" = $TEST_STR ]; then

== is a bash extension. ==bash扩展名。 = is the standard POSIX syntax, which is supported by both bash and zsh . =是标准的POSIX语法, bashzsh都支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM