Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unset all environment variables starting with a leading string while in a script (without closing or restarting bash)

Tags:

bash

shell

I'd like to:

unset myvarname*

where myvarname is a string and * is... well, you got it.

Tried

env | grep string | unset

but doesn't work.

I'm into a script and I don't want to start a new shell so no env -i or source something or leaving the reentering the shell

Please help a poor noob

Thanks in advance

like image 808
Kabu Avatar asked Mar 30 '17 15:03

Kabu


1 Answers

In the Bash shell, the ${!prefix@} parameter expansion generates all variables that start with prefix.

${!prefix@} Expands to the names of variables whose names begin with prefix [...] When @ is used and the expansion appears within double quotes, each variable name expands to a separate word.

This list can then be passed to unset:

unset "${!myvarname@}"
like image 138
Ingo Karkat Avatar answered Oct 08 '22 04:10

Ingo Karkat