CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Environment & Shell / unset

unset

Remove variables and functions from the shell

Beginnershellenvironment

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.

Syntaxunset [-v] NAME... | unset -f FUNCTION...

Watch it run

You exported short-lived AWS keys to test something. Remove them so the CLI goes back to your normal profile.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
env | grep -c '^AWS_'

Four AWS variables are exported in this shell: they override ~/.aws/config for every aws, terraform or SDK call you make.

Anatomy of a command

$
commandunset

Bash builtin: removes names from the current shell.

How it works

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.

Options you'll actually use

unset NAMERemove a variable (or a function, if no such variable)
-v NAMEOnly remove variables
-f NAMEOnly remove functions
NAME=Not unset: set to the empty string
${NAME-x} / ${NAME:-x}Default if unset / default if unset or empty
env -u NAME cmdRemove NAME for one command only

Common mistakes

unset $VAR

The $ expands first, so bash tries to unset a variable named after the value (or nothing at all).

unset VAR
Setting VAR= and assuming programs treat it as missing

An empty variable still exists and is still exported; tools check 'is it set', not 'is it non-empty'.

unset VAR
Assuming unset removes a secret from running processes

Each running program keeps the copy of the environment it started with.

Restart processes that received it, and rotate the secret if it leaked
unset PATH to 'reset' it

Nothing can be found by name until PATH is set again.

export PATH="/usr/local/bin:/usr/bin:/bin" # or open a new shell

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create and export TMP_TOKEN=abc, prove a child process sees it, then unset it and prove it's gone.

  2. 2

    Show the difference between unset and empty using ${VAR-unset}.

  3. 3

    Run 'bash -c "echo $HOME"' with HOME removed for that one command only, without changing your shell.

Related commands

exportPass variables to the programs you startenvShow the environment, or run a command with a changed onesetShell options: set -euo pipefail, set -x and friendstypeSee exactly what the shell will run for a name
PrevioussetNextwhich
Linux / Environment & Shell / unset

unset

Remove variables and functions from the shell

Beginnershellenvironment

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.

Syntaxunset [-v] NAME... | unset -f FUNCTION...

Watch it run

You exported short-lived AWS keys to test something. Remove them so the CLI goes back to your normal profile.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
env | grep -c '^AWS_'

Four AWS variables are exported in this shell: they override ~/.aws/config for every aws, terraform or SDK call you make.

Anatomy of a command

$
commandunset

Bash builtin: removes names from the current shell.

How it works

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.

Options you'll actually use

unset NAMERemove a variable (or a function, if no such variable)
-v NAMEOnly remove variables
-f NAMEOnly remove functions
NAME=Not unset: set to the empty string
${NAME-x} / ${NAME:-x}Default if unset / default if unset or empty
env -u NAME cmdRemove NAME for one command only

Common mistakes

unset $VAR

The $ expands first, so bash tries to unset a variable named after the value (or nothing at all).

unset VAR
Setting VAR= and assuming programs treat it as missing

An empty variable still exists and is still exported; tools check 'is it set', not 'is it non-empty'.

unset VAR
Assuming unset removes a secret from running processes

Each running program keeps the copy of the environment it started with.

Restart processes that received it, and rotate the secret if it leaked
unset PATH to 'reset' it

Nothing can be found by name until PATH is set again.

export PATH="/usr/local/bin:/usr/bin:/bin" # or open a new shell

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create and export TMP_TOKEN=abc, prove a child process sees it, then unset it and prove it's gone.

  2. 2

    Show the difference between unset and empty using ${VAR-unset}.

  3. 3

    Run 'bash -c "echo $HOME"' with HOME removed for that one command only, without changing your shell.

Related commands

exportPass variables to the programs you startenvShow the environment, or run a command with a changed onesetShell options: set -euo pipefail, set -x and friendstypeSee exactly what the shell will run for a name
PrevioussetNextwhich