简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM