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:
- List your variables. For example, depending on your scope you can do it with
env
orcompgen -v
. - Filter for desired variables. For example with
grep
orsed
. - 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
Related videos on Youtube
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, 2022Comments
-
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 over 8 yearsIt should be the only way. Thanks!
-
Rufus almost 4 yearsFor 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 almost 4 yearsThat 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