bash 我应该如何导出 http_proxy 变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13098634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:53:13  来源:igfitidea点击:

How should I export http_proxy variable?

bashnetworkingproxy

提问by Jofsey

I'm trying to write a simple script that will set proxy settings. Actually I just need to export http_proxy ftp_proxy https_proxy ...variables with exportcommand. But it's not working when I run it manually from the shell because exportaffect only current shell and subshells, but no others. Also I don't want to call it from .bashrcbecause it's not my default proxy settings.

我正在尝试编写一个简单的脚本来设置代理设置。其实我只需要用命令导出http_proxy ftp_proxy https_proxy ...变量export。但是当我从 shell 手动运行它时它不起作用,因为export只影响当前的 shell 和子 shell,而不影响其他的。另外我不想调用它,.bashrc因为它不是我的默认代理设置。

So how should I export http_proxyvariable to make effect globally?

那么我应该如何导出http_proxy变量以使其全局生效?

回答by Ashish Kumar

Back in the day I was also sick of setting and then unsetting the proxy settings after my work was done. I always wished if there was a command simple command to do the set and unset function for me.

回到那天,我也厌倦了在我的工作完成后设置然后取消设置代理设置。我一直希望是否有一个简单的命令来为我执行设置和取消设置功能。

Then I figured that if I create a new function in my .bashrc I can call it from the command line by using the bash-tab-completion. Saves even more time.

然后我想如果我在我的 .bashrc 中创建一个新函数,我可以使用 bash-tab-completion 从命令行调用它。节省更多时间。

This is what I did:

这就是我所做的:

$ vi ~/.bashrc
function setproxy() {
    export {http,https,ftp}_proxy='http://proxy-serv:8080'
}

function unsetproxy() {
    unset {http,https,ftp}_proxy
}

$ . ~/.bashrc

Now I just do:

现在我只做:

$ setproxy

or

或者

$ setp<TAB> and <ENTER>

and it sets the proxy for me. Hope this helps.

它为我设置了代理。希望这可以帮助。

回答by jfg956

Instead of doing this in a script, make this a function. You can declare this function in your .bashrc:

不要在脚本中执行此操作,而是将其设为函数。您可以在您的.bashrc

function set_custom_proxy() {
  export http_proxy='http://myproxy:3128'
}

Then run this in the current shell:

然后在当前 shell 中运行它:

echo $http_proxy
set_custom_proxy
echo $http_proxy

It works as a variable modification in a function is not local to the function.

它用作函数中的变量修改,而不是函数的局部变量。

EDIT:

编辑

FYI: to use a local variable in a function, you need to use the localkeyword:

仅供参考:要在函数中使用局部变量,您需要使用local关键字:

atest="Hello World"
btest="Hello World2"
function my_func() {
  local atest;
  atest="Hello World3"
  btest="Hello World4"
  echo $atest
  echo $btest
}
echo $atest
echo $btest
my_func
echo $atest
echo $btest

回答by P.P

Since you can't access .bashrc, you can use sourcecommand which will run in the current shell's context and all the variables you set will be available.

由于您无法访问.bashrc,您可以使用source将在当前 shell 上下文中运行的命令,并且您设置的所有变量都将可用。

source ./script

回答by P.P

If you don't want to modify the .bashrc file run your script with .

如果您不想修改 .bashrc 文件,请运行您的脚本 .

. script.sh