Chris Dobbyn
Chris Dobbyn

Reputation: 1

How do you unset all variables based on a prefix in zsh?

In bash you can do something like this:

unset "${!AWS_@}"

But this will give a substitution error in zsh.

I haven't found a really great way to do this in zsh. Presumably because zsh has a different expansion/substitution than bash. I'm assuming I would have to do a lookup and then loop through the return. I'm wondering if someone has a nice one liner (or has ran into this themselves).

Upvotes: 0

Views: 471

Answers (2)

chepner
chepner

Reputation: 531430

Use the -m flag.

unset -m "AWS_*"

From the entry for unset in man zshbuiltins:

If the -m flag is specified the arguments are taken as patterns (should be quoted) and all parameters with matching names are unset. Note that this cannot be used when unsetting associative array elements, as the subscript will be treated as part of the pattern.

Upvotes: 2

Chris Dobbyn
Chris Dobbyn

Reputation: 1

This works, but is pretty gross:

unset $(printenv | grep AWS_ | awk -F \= '{print $1}' | tr '\n' ' ')

This also works but is more gross:

for i in (printenv | grep AWS_ | awk -F \= '{print $1}')
  unset $i
end

Upvotes: 0

Related Questions