0

I'm pretty new to bash scripting. I'm trying to make a script to export the http_proxy variables. This is what I do from the terminal:

$export http_proxy=http://proxy21.iitd.ernet.in:3128/
$export https_proxy=https://proxy21.iitd.ernet.in:3128/

This works just fine. Now, here's my script (called setproxy):

#!/usr/bin/env bash
if [ $1 -eq 22 ]
then
    export http_proxy=http://proxy22.iitd.ernet.in:3128/
    export https_proxy=https://proxy22.iitd.ernet.in:3128/
elif [ $1 -eq 21 ]
then
    export http_proxy=http://proxy21.iitd.ernet.in:3128/
    export https_proxy=https://proxy21.iitd.ernet.in:3128/
elif [ $1 -eq 61 ]
then
    export http_proxy=http://proxy61.iitd.ernet.in:3128/
    export https_proxy=https://proxy61.iitd.ernet.in:3128/
elif [ $1 -eq 62 ]
then
    export http_proxy=http://proxy62.iitd.ernet.in:3128/
    export https_proxy=https://proxy62.iitd.ernet.in:3128/
fi

Essentially, I want to set the appropriate proxy server depending on the input. I put it in the bin folder, made it executable, added bin to the path, logged in and out. The terminal accepts setproxy as a valid command (at leat there's no command not found error) But, when I do:

$setproxy 22

There is no effect. The proxy remains unchanged. What am I doing wrong?

4

When you call the script, a new child shell is invoked to run it. Its proxies are set, but the proxy of the parent process (your shell) can't be changed from a child process. Try sourcing the script, i.e. call it like

. setproxy 21

Then the script will be interpreted by your current shell.

  • That worked. Thanks! Is there any way to avoid the . at the start? – hoodakaushal Aug 26 '14 at 10:40
  • The . is what makes it work. – choroba Aug 26 '14 at 10:42
  • Oh, ok. One more thing, how can I set the system wide proxy (ie, the one when I do Network -> Network Proxy -> (Set the proxy) -> Apply System wide) – hoodakaushal Aug 26 '14 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.