unsetRemove variables and functions from the shell
unset deletes a shell variable (and its exported copy for future child processes) or a function. You use it to clear secrets after use, to test how a script behaves when a variable is missing, and to undo a bad PATH or proxy setting.
unset [-v] NAME... | unset -f FUNCTION...You exported short-lived AWS keys to test something. Remove them so the CLI goes back to your normal profile.
unsetBash builtin: removes names from the current shell.
unset removes the name from bash's variable table, so it's no longer set at all, which is different from being set to an empty string. Because the environment for child processes is rebuilt from exported variables at each execve, an unset variable is simply absent from every program you start afterwards. Programs already running keep their copy. unset NAME removes a variable if one exists, otherwise a function with that name. -v and -f make it explicit. readonly variables can't be unset ('cannot unset: readonly variable'). Inside a function, unset on a local variable reveals the global one of the same name again. The difference between unset and empty matters to tools: ${VAR-default} only uses the default when VAR is unset, ${VAR:-default} also when it's empty, and many programs (proxies, AWS credentials) treat an empty variable differently from a missing one. unset PATH or unset HOME breaks command lookup and ~ in that shell until you set them again.
unset NAMERemove a variable (or a function, if no such variable)-v NAMEOnly remove variables-f NAMEOnly remove functionsNAME=Not unset: set to the empty string${NAME-x} / ${NAME:-x}Default if unset / default if unset or emptyenv -u NAME cmdRemove NAME for one command onlyunset $VARThe $ expands first, so bash tries to unset a variable named after the value (or nothing at all).
unset VARSetting VAR= and assuming programs treat it as missingAn empty variable still exists and is still exported; tools check 'is it set', not 'is it non-empty'.
unset VARAssuming unset removes a secret from running processesEach running program keeps the copy of the environment it started with.
Restart processes that received it, and rotate the secret if it leakedunset PATH to 'reset' itNothing can be found by name until PATH is set again.
export PATH="/usr/local/bin:/usr/bin:/bin" # or open a new shellTry these on a Linux machine of your own. Each task has a hint and a solution.
Create and export TMP_TOKEN=abc, prove a child process sees it, then unset it and prove it's gone.
Show the difference between unset and empty using ${VAR-unset}.
Run 'bash -c "echo $HOME"' with HOME removed for that one command only, without changing your shell.
unsetRemove variables and functions from the shell
unset deletes a shell variable (and its exported copy for future child processes) or a function. You use it to clear secrets after use, to test how a script behaves when a variable is missing, and to undo a bad PATH or proxy setting.
unset [-v] NAME... | unset -f FUNCTION...You exported short-lived AWS keys to test something. Remove them so the CLI goes back to your normal profile.
unsetBash builtin: removes names from the current shell.
unset removes the name from bash's variable table, so it's no longer set at all, which is different from being set to an empty string. Because the environment for child processes is rebuilt from exported variables at each execve, an unset variable is simply absent from every program you start afterwards. Programs already running keep their copy. unset NAME removes a variable if one exists, otherwise a function with that name. -v and -f make it explicit. readonly variables can't be unset ('cannot unset: readonly variable'). Inside a function, unset on a local variable reveals the global one of the same name again. The difference between unset and empty matters to tools: ${VAR-default} only uses the default when VAR is unset, ${VAR:-default} also when it's empty, and many programs (proxies, AWS credentials) treat an empty variable differently from a missing one. unset PATH or unset HOME breaks command lookup and ~ in that shell until you set them again.
unset NAMERemove a variable (or a function, if no such variable)-v NAMEOnly remove variables-f NAMEOnly remove functionsNAME=Not unset: set to the empty string${NAME-x} / ${NAME:-x}Default if unset / default if unset or emptyenv -u NAME cmdRemove NAME for one command onlyunset $VARThe $ expands first, so bash tries to unset a variable named after the value (or nothing at all).
unset VARSetting VAR= and assuming programs treat it as missingAn empty variable still exists and is still exported; tools check 'is it set', not 'is it non-empty'.
unset VARAssuming unset removes a secret from running processesEach running program keeps the copy of the environment it started with.
Restart processes that received it, and rotate the secret if it leakedunset PATH to 'reset' itNothing can be found by name until PATH is set again.
export PATH="/usr/local/bin:/usr/bin:/bin" # or open a new shellTry these on a Linux machine of your own. Each task has a hint and a solution.
Create and export TMP_TOKEN=abc, prove a child process sees it, then unset it and prove it's gone.
Show the difference between unset and empty using ${VAR-unset}.
Run 'bash -c "echo $HOME"' with HOME removed for that one command only, without changing your shell.