How unset a lot of environment variables

13,378

Solution 1

unset takes multiple variables:

unset HTTP_PROXY HTTPS_PROXY FTP_PROXY ALL_PROXY NO_PROXY

Solution 2

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.

Solution 3

Using babashka:

bb -o '(->> (System/getenv)
            keys
            (filter #(str/ends-with? % "_PROXY"))
            (map #(str "unset " %)))' | 
  source /dev/stdin
Share:
13,378

Related videos on Youtube

Bruno Wego
Author by

Bruno Wego

Problem Solver. Collaborator. Resourceful. Resilient. Detail-Oriented. Enthusiastic. Self-Motivated. Self-Taught. 18 years on the software road.

Updated on September 04, 2022

Comments

  • Bruno Wego
    Bruno Wego over 1 year

    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
    Bruno Wego over 8 years
    It should be the only way. Thanks!
  • Rufus
    Rufus almost 4 years
    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..
  • that other guy
    that other guy almost 4 years
    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