How to export http_proxy variable?

I am trying to write a simple script that will set proxy settings. Actually, I just need to export the http_proxy ftp_proxy https_proxy ... using the export command. But it does not work when I start it manually from the shell, because export only affects the current shell and subshells, but there are no others. Also I do not want to call it from .bashrc , because these are not my default proxy settings.

So, how do I export the http_proxy to make a global effect?

+4
source share
4 answers

On the same day that my work was finished, I was also tired of the settings, and then the proxy settings were turned off. I always wanted if the command was a simple command to execute the set and unset functions for me.

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

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 he sets up a proxy for me. Hope this helps.

+13
source

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

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

Then run this in the current shell:

 echo $http_proxy set_custom_proxy echo $http_proxy 

It works as a modification of a variable in a function that is not a local function.

EDIT

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

 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 
+4
source

Since you cannot access .bashrc , you can use the source command, which will be run in the current shell context, and all the variables you set will be available.

 source ./script 
+1
source

If you do not want to modify the .bashrc file, run the script with .

 . script.sh 
0
source

Source: https://habr.com/ru/post/1442458/


All Articles