38

Have any way to unset different variables using a one command?

unset HTTP_PROXY
unset HTTPS_PROXY
unset FTP_PROXY
unset ALL_PROXY
unset NO_PROXY
Bruno Wego
  • 2,099
  • 3
  • 21
  • 38

3 Answers3

63

unset takes multiple variables:

unset HTTP_PROXY HTTPS_PROXY FTP_PROXY ALL_PROXY NO_PROXY
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 3
    It should be the only way. Thanks! – Bruno Wego Aug 22 '15 at 20:56
  • For some reason, this does not work if the variables you want to unset is stored in a variable itself, e.g. `unset "$UNSET_VARS"` does not work.. – Rufus Jul 15 '20 at 05:36
  • 2
    That passes them all as a single parameter. You need each variable as a separate argument, e.g. by not quoting it, or preferably by using an array in the first place – that other guy Jul 15 '20 at 06:24
14

A little bit late, but anyway. Depending on your variable pattern you can shorten your unset:

  1. List your variables. For example, depending on your scope you can do it with env or compgen -v.
  2. Filter for desired variables. For example with grep or sed.
  3. Pass the variables to unset.

For example in your case it can be:

unset $(compgen -v | grep "_PROXY$")

It's not exactly one command, but it imitates unset *_PROXY, as you requested in your comment.

Anton Bessonov
  • 9,208
  • 3
  • 35
  • 38
  • I added the `-i` argument to the `grep` command to ignore case. I found I had a number of proxy related environment variables set which were also lower case. As it's written above it'll only catch the ones with proxy in all uppercase characters. – PicoutputCls Mar 02 '23 at 16:07
3

Using babashka:

bb -o '(->> (System/getenv)
            keys
            (filter #(str/ends-with? % "_PROXY"))
            (map #(str "unset " %)))' | 
  source /dev/stdin
Michiel Borkent
  • 34,228
  • 15
  • 86
  • 149