Environment variables are dynamic values that the operating system and applications use to determine settings and configuration options.

These are used by applications launched in shells or sub-shells. These variables have a name and their respected value.

Setting Environment Variables in Linux

  1. Temporary Environment Variables: Set for the duration of the terminal session.

    VARIABLE_NAME=value
    
    • Example:

      MY_VAR="Hello"
      echo $MY_VAR  # Output: Hello
      
  2. Permanent Environment Variables: Set permanently for all sessions.

    • Add the export command to profile files such as .bashrc, .bash_profile, or /etc/profile for system-wide settings.

    • Syntax:

      export VARIABLE_NAME=value
      
    • Example (add to .bashrc):

      export MY_VAR="Hello"
      
  3. Viewing Environment Variables:

    • Use the printenv or env command to list environment variables.
  4. Unsetting Environment Variables:

    • Remove an environment variable using the unset command.

    • Example:

      unset VARIABLE_NAME
      
      
  5. Modifying Environment Variables:

    • Update the value of an existing environment variable.

    • Syntax:

      export VARIABLE_NAME=new_value
      
      
    • Example:

      export MY_VAR="NewValue"