【问题标题】:Why is my bash script not exporting to my environment?为什么我的 bash 脚本没有导出到我的环境中?
【发布时间】:2018-01-08 23:10:12
【问题描述】:

我有一个简单的小 bash 脚本,它会要求输入密码并将其导出到环境中:

printf "Proxy authentication failed.\n"
read -p "Enter Password to try again: " mypassword
printf "Proxy authentication succeeded\n"
export PASSWORD="mypassword"

但是当我尝试运行它时,它不会导出到环境中:

baal@baal-Aspire-5733Z:/tmp$ sh vaWfKh.sh
Proxy authentication failed.
Enter Password to try again: test
Proxy authentication succeeded
baal@baal-Aspire-5733Z:/tmp$ printenv
CLUTTER_IM_MODULE=xim
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;
...
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
PATH=/home/baal/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/baal/.rvm/bin:/home/baal/.rvm/bin
LESSOPEN=| /usr/bin/lesspipe %s
GTK_IM_MODULE=ibus
_=/usr/bin/printenv

如何通过 bash 脚本导出到环境变量?

【问题讨论】:

    标签: bash export environment-variables


    【解决方案1】:

    您需要source 脚本或在其前面运行. 以便在脚本期间导出变量 -

    注意:您需要插入 mypassword 变量才能将其设置为您的环境变量。

    假设这是你的./myscript.sh

    #!/bin/bash
    
    printf "Proxy authentication failed.\n"
    read -p "Enter Password to try again: " mypassword
    printf "Proxy authentication succeeded\n"
    export PASSWORD=${mypassword}
    

    在您的终端中运行:

    . ./myscript.sh
    

    或者

    source ./myscript.sh
    

    【讨论】:

    • @arm67146 您是否尝试更新脚本以包含插值?我在本地测试了这个更新的脚本,它对我有用。
    • 嘿,是的,我想通了,我正在通过另一个应用程序调用它,但路径变得一团糟。谢谢它的工作原理!
    • 没问题 :) 我很高兴它有帮助。
    猜你喜欢
    • 2021-07-11
    • 2018-12-18
    • 2023-03-11
    • 2022-11-14
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    相关资源
    最近更新 更多