2017-08-01 63 views
1

我有一个简单的小bash脚本,将要求输入密码,并将其导出到环境:为什么我的bash脚本不能导出到我的环境中?

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

然而,当我尝试运行它,它不会导出到环境:

[email protected]:/tmp$ sh vaWfKh.sh 
Proxy authentication failed. 
Enter Password to try again: test 
Proxy authentication succeeded 
[email protected]:/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脚本导出到环境变量?

回答

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 
+0

,不能正常工作,采购它,我的意思 – arm67146

+0

@ arm67146您是否尝试更新脚本以包含插值?我在本地测试了这个更新后的脚本,它为我工作。 –

+0

嘿,是的,我想通了,我通过另一个应用程序调用它,路径越来越混乱。谢谢你的作品! – arm67146

相关问题