How can i unset a variable which has dot in it for example: s3.protocol
I tried to unset with the following command and i get the error
unset s3.protocol
bash: unset: `s3.protocol': not a valid identifier
env | grep -i protocol
amazon.s3.protocol=HTTPS
s3.protocol=HTTPS
elasticsearch.protocol=https
If your variable appears in env
, you can replace the current shell with another shell that does not have the environment variable in it. Assuming you want to remove all three variables.
exec env -u amazon.s3.protocol -u s3.protocol -u elasticsearch.protocol "$SHELL"
The env -u
removes the specified variables before launching another copy of the current shell. I used $SHELL
here for illustration but you should replace it with the name or path to your actual shell (which for example can be bash
or /usr/local/bin/bash-4
), or you can use $0
if it makes sense. The exec
ensures that the new shell replaces the current shell instead of being launched as a subshell.
To also forward all command line arguments of the current shell and all the variables local to the current shell, some extra arguments can be added.
exec env -u amazon.s3.protocol -u s3.protocol -u elasticsearch.protocol "$SHELL" "$@" --rcfile <(echo ". ~/.bashrc; $(declare -p| awk '($2 !~ "r")')")
Where "$@"
forwards all the command line arguments to the new shell (assuming shift
has not been used directly in the current shell), and the $(declare -p)
extracts all the bash variables in the current shell. The output of declare -p
is passed to awk
to remove readonly variables, then passed to the new shell via the mechanism presented in this answer.