How to unset exported variables?

To unset (remove) exported variables in a Linux shell, you can use the unset command, just like you would for local variables. Here’s how to do it:

Steps to Unset Exported Variables

  1. Unset the Exported Variable: Use the unset command followed by the variable name to remove the exported variable.

    unset VARIABLE_NAME

    Example:

    export MY_VAR="Hello, World!"
    echo $MY_VAR  # Outputs: Hello, World!
    unset MY_VAR
  2. Verify the Variable is Unset: After unsetting the variable, you can check if it has been successfully removed by using the echo command.

    echo $VARIABLE_NAME

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

    Example:

    echo $MY_VAR  # Outputs nothing, indicating the variable has been unset
  3. Check with the env Command: You can also use the env command to confirm that the variable is no longer listed among the environment variables.

    env | grep VARIABLE_NAME

    If the variable has been unset, this command will yield no results.

    Example:

    env | grep MY_VAR  # Outputs nothing, confirming that MY_VAR is not set

Summary

  • Use unset VARIABLE_NAME to remove an exported variable.
  • Check with echo $VARIABLE_NAME to see if it outputs nothing.
  • Use env | grep VARIABLE_NAME to confirm it is not listed among environment variables.

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

0 Comments

no data
Be the first to share your comment!