Remove environment and shell variables or functions from the current shell quickly and safely. 22.05.2026 | reading time: 2 min The shell builtin `unset` removes a variable or a function from the current shell environment; simple yet powerful. Try it to clean up temporary state, avoid accidental inheritance, or remove secrets from a session. A quick cleanup example Create and export a variable with `export MYVAR=hello`, confirm with `echo $MYVAR` which prints `hello`, then run `unset MYVAR` and `echo $MYVAR` prints nothing because the name no longer exists; note the difference to `MYVAR=` which sets an empty value but keeps the name in the environment. Functions, readonly and errors Remove a shell function with `unset -f myfunc` after defining `myfunc() { echo hi; }`, but beware: `unset` cannot remove a readonly variable and the shell will report an error and return a nonzero status if he tries to do so. Scope and common gotchas `unset` affects only the current shell or script, so running it in a subshell will not change the parent shell; `unset` cleans a name entirely while `VAR=` leaves an empty string, and different shells implement flags slightly differently so test scripts for portability. Practical habits Use `unset` to remove credentials or temporary names at the end of scripts, prefer unsetting in the same execution context that created the variable, and check return codes when removing readonly or protected names to handle failures. Where to go next Master the small builtins and the shell will reward with cleaner scripts and fewer surprises; explore more shell tools and consider certifications like CompTIA Linux+ or LPIC-1 and intensive exam preparation at bitsandbytes.academy to level up fast. Join Bits & Bytes Academy First class LINUX exam preparation. utilities scripting security