1

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.

  • 1
    What are all those backticks for? – thrig Oct 27 '17 at 1:29
  • 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 Oct 27 '17 at 1:31
  • 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 – user257636 Oct 27 '17 at 1:32
  • 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) – thrig Oct 27 '17 at 1:33
  • 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` – user257636 Oct 27 '17 at 1:35
6

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
  • 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! – Chris S Feb 20 at 8:30

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.