Environment variables are an essential part of any Linux system. They store information about the user‘s environment and configuration settings. Knowing how to properly manage environment variables is crucial for any Linux power user or system administrator.

This comprehensive guide will teach you how to remove, add, and modify environment variables in Linux using the bash shell.

What are Environment Variables?

Environment variables are dynamic values that define the properties of a user‘s shell session. Some common examples include:

  • $HOME – The path to the user‘s home directory
  • $USER – The username of the current user
  • $PATH – Directories searched for executable programs

Environment variables control many aspects of the shell‘s behavior. They are useful for storing configuration options and sharing data between programs.

Viewing Environment Variables

To view all environment variables for the current session, use the printenv or env commands:

printenv
env

To view the value of a specific variable, use echo:

echo $HOME

Removing Environment Variables with unset

To permanently remove an environment variable, use the unset command:

unset VARIABLE

For example, to remove the EDITOR variable:

unset EDITOR

After unsetting a variable, it will no longer be defined in the current shell session.

Modifying Environment Variables

To modify the value of an existing environment variable, assign a new value:

VARIABLE=value

For example, to add a new path to the PATH variable:

PATH=$PATH:/new/path 

The change will only apply to the current shell process. To make permanent changes, add the command to your shell‘s profile script (e.g. ~/.bashrc).

Adding New Environment Variables

To set a new environment variable, use the export command:

export VARIABLE=value

For example:

export EDITOR=vim

The variable will be available in the current shell session and spawned child processes. To make the change apply permanently across login sessions, add export VAR=value to your shell‘s profile script.

Managing Environment Variable Scope

Environment variables can have different scopes:

  • Local variables only exist within the current shell process or script.
  • User variables apply to a single user and are set in ~/.bashrc.
  • System variables apply globally to all users and are set in /etc/environment.

In summary, environment variables are a powerful tool for customizing your Linux environment. Learning to properly manage them will allow you to boost your productivity and streamline system administration tasks.

Similar Posts