在 archlinux shell 中导出环境
当我在公司时,我必须导出3个环境变量,http_proxy,https_proxy,all_proxy,
我写了一个像这样的文件〜/ bin / setproxy
#! /bin/sh
export http_proxy=http://......:8888
export https_proxy=http://......:8888
export all_proxy=http://......:8888
,但是当我在bash中执行这个文件时,然后使用env | grep http_proxy ,我什么也没得到。 但是“source ~/bin/setproxy”可以工作,但是有什么方法可以将其缩短为 1 个字命令。 我写了另一个文件,只有1行,
source ~/bin/setproxy
但它不起作用。
when I at company , I have to export 3 enviroment variables, http_proxy,https_proxy,all_proxy,
I wrote a file ~/bin/setproxy like this
#! /bin/sh
export http_proxy=http://......:8888
export https_proxy=http://......:8888
export all_proxy=http://......:8888
but when I execute this file at bash, then use env | grep http_proxy , I got nothing.
but "source ~/bin/setproxy" works, but is there any way short this to 1 word command.
I wrote another file only 1 line,
source ~/bin/setproxy
but it does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您执行该脚本时,会生成一个子 shell,并在该 shell 中执行三个导出,当脚本完成时,子 shell 退出,这就是为什么您看不到设置的环境变量。
您可以将该代码放入一个函数中,例如在
.bashrc
中,然后调用该函数,这样它就可以工作,如下所示:When you execute that script a sub-shell is spawned and the three export are perfomed in that shell, when the script finishes, the sub-shell exits, that's why you don't see the environment variables as set.
You could put that code in a function, say in your
.bashrc
, and call that, this way it will work, something like the following:我认为你的问题是因为你正在执行:
或者:
在两种这些情况下,它们在子shell中运行,这意味着导出是在该子shell中,而不是你的shell正在给他们打电话。
您可以使用缩写形式
source
:创建别名:
或在
.bashrc
或其他启动脚本中。后一个解决方案将允许您执行:
完成工作。
I think your problem is because you are executing either:
or:
In both those cases, they run in a subshell which means the export is in that subshell, not the shell you're calling them from.
You can either use the short form of
source
:or create an alias:
in your
.bashrc
or other startup scripts.That latter solution will allow you to just execute:
to do the work.