Linux

How to Set Environment Variables in Linux

Linux Environment Variables

Environment variables are dynamic-named values that can influence the behavior of running processes on a computer. They exist in every computer system and are used to make the system aware of certain conditions. In Linux, environment variables play a crucial role in configuring software applications, storing sensitive information, and setting system-wide preferences. This guide will teach you how to set, list, print, and make environment variables persistent in Linux.

What Are Environment Variables?

Environment variables are name-value pairs used by a system’s shell to define default shell properties. Some of these include your shell’s home directory, prompt, and current working directory. Environment variables are inherited by sub-shells and are available to applications and daemons. You can also create and set your own environment variables.

Environment Variable Scope

A variable’s scope refers to the parts of a program or environment that can access a given variable. An environment variable in Linux can have a global or local scope.

  1. Globally scoped environment variables are accessible from anywhere in a particular environment bound by the terminal.
  2. Locally scoped environment variables can only be accessed by the terminal that defines the variable. They cannot be accessed by any program or script.

Differences Between Environment and Shell Variables

Standard UNIX variables are classified into two categories: environment variables and shell variables.

  1. Environment variables are available and valid system-wide. They can be used by scripts and applications and are inherited by all spawned child processes and shells. By convention, environment variables are given uppercase names.
  2. Shell variables are available only in the current shell session. They are useful when you need to store values temporarily. Each shell, such as zsh and bash, has its own set of internal shell variables.

Commonly Used (Global) Environment Variables

The following environment variables are commonly available to most popular Linux systems by default:

  1. USER: The currently logged-in username.
  2. HOME: The path to the current user’s home directory.
  3. SHELL: The pathname of the current user’s shell.
  4. PATH: A list of directories that the shell searches for executable files.
  5. PWD: The path to your current working directory (PWD stands for “Print Working Directory”).
  6. UID: The current user’s unique identifier.

How to Set Environment Variables in Linux

Temporary Environment Variables

To set an environment variable temporarily, which will only last for the duration of the shell session, you can use the export command:

For example, to set the JAVA_HOME environment variable temporarily:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

Persistent Environment Variables for a Single User

To make an environment variable persistent across sessions for a single user, add the export command to the user’s profile script, such as ~/.bashrc, ~/.bash_profile, or ~/.profile:

echo 'export VAR_NAME="value"' >> ~/.bashrc

For example:

echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64' >> ~/.bashrc

After adding the variable, you can apply the changes immediately by sourcing the profile script:

source ~/.bashrc

Persistent Environment Variables for All Users

To set an environment variable globally for all users, you can create a script in the /etc/profile.d/ directory:

sudo echo 'export VAR_NAME="value"' > /etc/profile.d/myenv.sh

For example:

sudo echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64' > /etc/profile.d/java.sh

Make sure the script is executable:

sudo chmod +x /etc/profile.d/myenv.sh

How to List Environment Variables in Linux

To list all environment variables, you can use the env, printenv, or set commands:

env

Or

printenv

Or

set

How to Print Environment Variables in Linux

There are two ways to print the already-defined environment variables:

  1. printenv VARIABLE_NAME
  2. echo $varname

For example, to print the value of the SHELL variable using both methods:

printenv SHELL

And

echo $SHELL

How to Unset Environment Variables

To remove an environment variable, use the unset command:

unset VAR_NAME

For example:

unset JAVA_HOME

Best Practices for Using Environment Variables

  1. Use descriptive names for your environment variables, so they clearly indicate their purpose.
  2. Keep the names and structures of your environment variables consistent across different environments (e.g., development, testing, production) to avoid confusion and potential errors.
  3. For application-specific variables, using a prefix can be a good practice. This can help you quickly identify the variables related to a specific application and prevent potential clashes with other variables.
  4. Consider using tools for managing environment variables, such as secret management or configuration management systems.

Conclusion

In this comprehensive guide, you learned how to create, define, list, print, and make environment variables persistent in Linux. You also learned about the different scopes of environment variables and best practices for using them. By understanding and effectively managing environment variables, you can ensure that software applications have the necessary configuration to operate correctly and securely.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button