I am trying to set a proxy on a rotating basis; but my export commands are not working. I'm using CentOS 7.

I've gone back to basics and tested a really simple bash script, which sets and unsets environment variables, and then tests them. The environment variables appear set properly, but my external IP is still showing as my local IP.

$1 is an address in the form


function set() {
    export http_proxy="$1/"    # not working
    export https_proxy="$1/"   # not working

function unset() {
    unset http_proxy
    unset https_proxy

function testExtIP() {
    externalHTTP=$(curl -s http://api.ipify.org) # an API that echos the external IP quickly
    externalHTTPS=$(curl -s https://api.ipify.org)
    echo "External HTTP: $externalHTTP; external HTTPS: $externalHTTPS" 

case "$1" in
    set $2 # speed
    unset $2
    testExtIP $2
    echo "Usage: $0 $versum [set|unset|test]"

I would like the script to return:

External HTTP:; external HTTPS:

But my external IP isn't set:

External HTTP: **SERVERIP**; external HTTPS: **SERVERIP**

which I've tested using various tools. Why is the export command not working? Am I missing something important?

marked as duplicate by Patrick, thrig, mdpc, Michael Homer, jimmij Jan 20 '18 at 12:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • this does not seem useful, as the exports are only in the context of the script, and vanish when the script exits (a child process cannot alter the environment of its parent) – thrig Jan 19 '18 at 22:33
  • Just some nitpicking: set and unset are shell built in commands. Just should pick other names for your functions. – Kusalananda Jan 20 '18 at 12:33

You can't do it like this. A script has its own set of variables (or environment if you will) which is only available to the script itself and that disappears as soon as the script ends. Therefore the shell which is the parent of the script (because you run the script from within the shell) doesn't know about the environment and set variables of the script.

  • This is helpful. I think here was my problem all along. A good solution would be to use these functions in the script that uses the proxy server? – Michael Riordan Jan 19 '18 at 22:50
  • No, the environment is still only available to the script itself so testing with the external server does not give the expected result. – wie5Ooma Jan 19 '18 at 22:57
  • And by external server I mean the API of course. – wie5Ooma Jan 19 '18 at 22:58

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