Concept Flow - Unsetting variables (unset)
Variable is set
Call unset command
Variable is removed
Variable no longer exists
The flow shows a variable initially set, then the unset command removes it, so it no longer exists.
name="Alice"
echo $name
unset name
echo $name| Step | Command | Variable 'name' Value | Output |
|---|---|---|---|
| 1 | name="Alice" | Alice | |
| 2 | echo $name | Alice | Alice |
| 3 | unset name | unset (no value) | |
| 4 | echo $name | unset (no value) |
| Variable | Start | After Step 1 | After Step 3 | Final |
|---|---|---|---|---|
| name | unset | Alice | unset | unset |
unset variable_name - Removes the variable completely - After unset, variable has no value - echo prints nothing if variable unset - Useful to clear variables in scripts
unset command do in a bash script?unsetunset command removes a variable completely from the shell environment, so it no longer exists.myVar in bash?unset variable_name without any symbols or parentheses.myVar="Hello" echo $myVar unset myVar echo $myVar
myVar is set to "Hello", so echo $myVar prints "Hello".unset myVar, the variable is removed. The second echo $myVar prints nothing (empty line).unset $myVar
unset $myVar expands $myVar to its value, causing unset to try to remove a variable named as that value, which is incorrect.var1="one" var2="two" var3="three"
var and end with an odd number (1 or 3). Which command correctly unsets these variables?Pass the test and this pattern moves unknown