How to unset a variable?

To unset (remove) a variable in a Linux shell, you can use the unset command. Here’s how to do it:

Unsetting a Variable

  1. Unset a Local Variable: If you have a local variable that you want to remove, you can use the following command:

    unset VARIABLE_NAME

    Example:

    flower=rose
    echo $flower  # Outputs: rose
    unset flower
    echo $flower  # Outputs nothing, indicating the variable has been unset
  2. Unset an Environment Variable: The same unset command can be used to remove an environment variable:

    unset VARIABLE_NAME

    Example:

    export nut=almond
    echo $nut  # Outputs: almond
    unset nut
    echo $nut  # Outputs nothing, indicating the variable has been unset

Verification

To verify that the variable has been successfully unset, you can use the echo command:

echo $VARIABLE_NAME

If the variable has been unset, this command will produce no output.

Important Note

  • Unsetting a variable only affects the current shell session. If the variable is defined in a configuration file (like .bashrc or .zshrc), it will be recreated the next time you start a new terminal session or source the file.

If you have any further questions or need clarification, feel free to ask!

0 Comments

no data
Be the first to share your comment!