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 export
command.
But it's not working when I run it manually from the shell because export
affect only current shell and subshells, but no others. Also I don't want to call it from .bashrc
because it's not my default proxy settings.
So how should I export http_proxy
variable to make effect globally?
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.
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.
Instead of doing this in a script, make this 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 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 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
Since you can't access .bashrc
, you can use source
command which will run in the current shell's context and all the variables you set will be available.
source ./script
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With