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

Time:01-06

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).

CodePudding user response:

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

CodePudding user response:

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.

  • Related