Bash Script - Setting Local Environment Variables (Proxy)

17,126

It looks like there are several mistakes here. First, your variable assignment expressions shouldn't be inside backticks. The backticks are a (somewhat dated) syntax for command substitution. I think what you were going for was single-quotes. Quoting variables is done to preserve string literals and prevent parameter substitution.

Also, you can't use the hyphen character in your variable names. Here is an excerpt from the Bash Manual, Chapter 2: Definitions:

name

A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier.

Also see the following posts for more information:

Here is what your scripts might look like after removing the backticks and the hyphens:

#!/bin/bash
# set_proxy.sh

export HTTP_PROXY='http://nick:nick@proxy.***.***.com:****'
export HTTPS_PROXY='http://nick:nick@proxy.***.***.com:****'
export http_proxy='http://nick:nick@proxy.***.***.com:****'
export https_proxy='http://nick:nick@proxy.***.***.com:****'

and:

#!/bin/bash
# unset_proxy.sh

unset HTTP_PROXY
unset HTTPS_PROXY
unset http_proxy
unset https_proxy

One final note: executing a script causes it to run in a subshell, so the environment variables won't affect your active shell session. See, for example, the following post:

The short answer is that you can't. So if you want your scripts to modify your current shell environment then you have to "source" them, e.g.:

source set_proxy.sh

source unset_proxy.sh
Share:
17,126

Related videos on Youtube

user257636
Author by

user257636

Updated on September 18, 2022

Comments

  • user257636
    user257636 over 1 year

    I am trying to create 2 scripts. One for setting local enviroment variables, and the other for unsetting local enviroment varibales.

    Firstly, both scripts were created under the following commands:

    touch set-proxy.sh touch unset-proxy.sh

    Then I made both of them executable:

    chmod 700 set-proxy.sh chmod 700 unset-proxy.sh

    Then I went into the set-proxy.sh file and put the following inside:

    #!/bin/bash
    
    `export HTTP_PROXY=http://nick:nick@proxy.***.***.com:****`
    `export HTTPS_PROXY=http://nick:nick@proxy.***.***.com:****`
    `export HTTP-PROXY=http://nick:nick@proxy.***.***.com:****`
    `export HTTPS-PROXY=http://nick:nick@proxy.***.***.com:****`
    `export http_proxy=http://nick:nick@proxy.***.***.com:****`
    `export https_proxy=http://nick:nick@proxy.***.***.com:****`
    `export http-proxy=http://nick:nick@proxy.***.***.com:****`
    `export https-proxy=http://nick:nick@proxy.***.***.com:****`
    

    Then I went inside the unset-proxy.sh file and put the following inside:

    #!/bin/bash
    
    `unset HTTP_PROXY`
    `unset HTTPS_PROXY`
    `unset HTTP-PROXY`
    `unset HTTPS-PROXY`
    `unset http_proxy`
    `unset https_proxy`
    `unset http-proxy`
    `unset https-proxy`
    

    I pretty much need a quick way to unset and set all proxy configuration. I thought creating two scripts would be the simplest way.

    However, when I run ./unset-proxy.sh or ./set-proxy.sh I get the following error messages:

    ./unset-proxy: line 5: unset: `HTTP-PROXY': not a valid identifier
    ./unset-proxy: line 6: unset: `HTTPS-PROXY': not a valid identifier
    ./unset-proxy: line 9: unset: `http-proxy': not a valid identifier
    ./unset-proxy: line 10: unset: `https-proxy': not a valid identifier
    

    (Then I get the same with the set-proxy.sh as well).

    If someone could help point me in the right direction of what I am doing wrong and how I can correct it, would be super appreciated. Thanks.

    • thrig
      thrig over 6 years
      What are all those backticks for?
    • user257636
      user257636 over 6 years
      I thought backticks were used in shell commands to tell the script to run an exxecutable. Even when i remove the, still doesn't work and still get the same error. Thanks for the question though.
    • user257636
      user257636 over 6 years
      Still get the error ./unset-proxy: line 5: unset: HTTP-PROXY': not a valid identifier ./unset-proxy: line 6: unset: HTTPS-PROXY': not a valid identifier ./unset-proxy: line 9: unset: http-proxy': not a valid identifier ./unset-proxy: line 10: unset: https-proxy': not a valid identifier
    • thrig
      thrig over 6 years
      Why are you using ./script (which creates its own process, which then goes away) versus . ./script (which runs the code within the context of the current process)
    • user257636
      user257636 over 6 years
      ah not too sure really. So when i try running ` . ./unset-proxy` i get the same error but in a different format: -bash: unset: HTTP-PROXY': not a valid identifier`
    • thrig
      thrig over 6 years
      All those have hyphens in their name. Are you sure a shell variable can contain a hyphen?
    • user257636
      user257636 over 6 years
      I'm not too sure, would have thought they could contain -, there must be a way to put - in a shell script, well i would have thought anyways.
  • Chris S
    Chris S about 5 years
    The last part of your comment was definitely the most helpful for me. Understanding that you cannot set variables for your terminal shell from inside a subshell saved me a lot of googling. Thanks!
  • Admin
    Admin about 2 years
    If your user/pwd and proxy address are the same to all proxies type, you can use this: export {all,tcp,http,https,socks,ALL,TCP,HTTP,HTTPS,SOCKS}_proxy='n‌​ick:nick@proxy.***.*‌​**.com:****'