how to set environment variables in linux with code examples

Environment variables are system-wide parameters used by the Linux operating system, applications, and different processes running within the system. These variables are groups of strings that contain relevant information, such as paths to directories, references to libraries, or configuration options.

Setting up environment variables in Linux has become a fundamental skill for system administrators, developers, and users. With this knowledge, you can adapt your environment to suit your needs, customize your bash shell environment, and organize your development and project environments.

In this article, we will cover the basics of environment variables in Linux, explain how you can set them, and give code examples of how to do so on different platforms.

Understanding Environment Variables

Environment variables hold a significant role in the Linux OS, and they can be accessed by the system shell, the kernel, or individual processes. A process can query an environment variable to retrieve critical information that can be used to configure its runtime behavior.

The following are a few examples of common environment variables:

  1. PATH – This environment variable specifies a search path for the shell to find executable files. When a user types the name of a command in a shell, the shell checks the directories in the PATH variable to find the command.

  2. HOME – This environment variable indicates the path to the current user's home directory.

  3. TERM – This variable defines the type of terminal that is being used.

  4. SHELL – This variable specifies the default shell for a user.

  5. PS1 – This variable is used to set up the prompt string for the shell.

These are just a few examples of the many environment variables that exist in the Linux OS.

Setting Environment Variables in Linux

Environment variables can be set in a few ways. The most common ways are by using export, set, or by adding them to the /etc/environment and ~/.bashrc files.

  1. Using Export:

Using the export command allows you to set an environment variable for a session or process. In other words, once you set the variable, it is only valid for the current session of the terminal.

To set an environment variable, use the following command:

export VAR_NAME=value

Replace VAR_NAME with the name of the environment variable you want to set and value with the desired value of the variable.

For example, to set the PATH environment variable to include the directory /usr/local/bin, use the following command:

export PATH=$PATH:/usr/local/bin

This command appends /usr/local/bin to the existing PATH variable.

  1. Using Set:

The set command is similar to export, but it sets variables that are only valid for the current shell environment. These variables are not exported and cannot be accessed by sub-shells.

To set a variable using the set command, use the following syntax:

var_name=value

Here is an example of how to use the set command to set a variable:

set MY_VAR=helloworld
  1. Using ~/.bashrc:

By adding definitions to the ~/.bashrc file, you can set environment variables for the system and user. Any variables that are defined in this file are available every time you open a bash shell.

Open the ~/.bashrc file in your favorite text editor and add your environment variable definitions. Here is an example of how to set the PATH environment variable in ~/.bashrc:

export PATH=$PATH:/usr/local/bin
  1. Using /etc/environment:

The /etc/environment file stores system-wide environment variables that are available to all users on the system. This file is usually used to set system-wide variables such as the locale or JAVA_HOME.

Open the /etc/environment file with a text editor and add your variable definition to it. Here is an example:

MY_VAR=helloworld

Code Examples

The following are some examples of how to set up environment variables in Linux:

  1. Ubuntu and Debian:

To set the environment variables in Ubuntu and Debian using the export command, open a terminal, and type:

export VAR_NAME=value

To set the environment variables persistently in the current user's environment, modify the ~/.bashrc file:

export VAR_NAME=value

To set the environment variables system-wide in Ubuntu and Debian, open the /etc/environment file with root privileges and add:

VAR_NAME=value

To set the environment variables in Ubuntu and Debian when using systemd, create a systemd unit file, and set the environment variables like this:

[Unit]
Description=My service

[Service]
Environment=VAR_NAME=value
  1. CentOS / RHEL:

To set environment variables in CentOS or RHEL, you can use the export command or modify the ~/.bashrc file:

export VAR_NAME=value

To set environment variables system-wide in CentOS or RHEL, open the /etc/environment file with root privileges and add:

VAR_NAME=value

You can also set environment variables when using systemd by creating a systemd unit file and setting the environment variables like this:

[Unit]
Description=My service

[Service]
Environment=VAR_NAME=value

Conclusion:

Environment variables are a crucial part of the Linux operating system. They allow you to customize your environment and configure your applications and processes. In this article, we covered some of the basics of setting environment variables in Linux, and provided some code examples on how to set them up on various platforms.

Now that you have learned how to set environment variables, you can use this knowledge to set up your applications and development environments to your exact requirements.

  1. Exporting Environment Variables:

Exporting environment variables is one of the most common ways to set up environment variables in Linux. The export command sets the variable for the current session, and any sub-shells spawned from the current shell will inherit the same environment variables.

To export an environment variable, we can use the following syntax:

export VAR_NAME=value

The VAR_NAME is the name of the environment variable you want to create or update, and value is the value you want to assign to the variable. For instance, if you want to add /usr/local/bin to your system's PATH environment variable, you can use this command:

export PATH=$PATH:/usr/local/bin

In this example, we are appending the /usr/local/bin directory to the current PATH environment variable, using the $PATH variable to reference the existing value of PATH.

  1. Using Set:

The set command is another way of setting up environment variables in Linux. However, it creates the variable for the current shell only, and any processes or sub-shells that are spawned from the current shell won't inherit the variable.

To create a variable using set, we can use the following syntax:

set VAR_NAME=value

Here, VAR_NAME is the name of the environment variable, and value is the value you want to assign to the variable. For example, to create a variable called MY_VAR with a value of helloworld, you can use this command:

set MY_VAR=helloworld
  1. Setting Environment Variables in Bash Configuration files:

Another way to set up environment variables on a Linux system is by modifying the bash configuration files. Bash is a popular shell used on Linux systems, and it provides two configuration files to customize the behavior of the shell: ~/.bashrc for user-specific settings and /etc/bashrc for system-wide settings.

To set an environment variable using these configuration files, we can use the following syntax:

export VAR_NAME=value

or

set VAR_NAME=value

Here, VAR_NAME is the name of the environment variable, and value is the value you want to assign to the variable.

If we want to set a system-wide environment variable, we can add the export command to the /etc/bashrc file, while for a user-specific environment variable, we can add it to the ~/.bashrc file.

For example, to add /usr/local/bin directory to the PATH environment variable for all users on the system, we can add the following line to the /etc/bashrc file:

export PATH=$PATH:/usr/local/bin
  1. Setting Environment Variables in Systemd:

Systemd is a popular initialization system used on many Linux distributions. It provides comprehensive support for managing system processes and services, including setting environment variables for a service.

To set an environment variable for a systemd service, we can use the Environment directive in the service unit file. The Environment directive allows us to specify a list of environment variables that will be set for the service.

Here's a sample systemd unit file that sets the MY_VAR environment variable for a service:

[Unit]
Description=My Service

[Service]
Environment=MY_VAR=helloworld
ExecStart=/usr/bin/myservice

[Install]
WantedBy=multi-user.target

In this example, we have added the line Environment=MY_VAR=helloworld to the Service section of the unit file. systemd will launch the service with the MY_VAR environment variable set to helloworld.

Conclusion:

Setting environment variables is an essential skill that every Linux user, administrator, and developer should know. In this article, we have covered the basics of environment variables, including how to set them using the export and set commands, how to create them in the bash configuration files, and how to set them for a systemd service.

By mastering these techniques, you can customize your environment to meet your needs, configure your applications, and optimize your Linux-based systems for improved productivity and performance.

Popular questions

Q1. What is an environment variable in Linux?
A1. An environment variable in Linux is a system-wide parameter that contains relevant information such as paths to directories, references to libraries, or configuration options.

Q2. What is the export command used for in Linux?
A2. The export command in Linux is used to set an environment variable for a session or process.

Q3. How do you set environment variables using the ~/.bashrc file in Linux?
A3. To set environment variables using the ~/.bashrc file in Linux, we can open the file in a text editor and add the export command followed by the variable name and value. For example, to set the PATH variable, we can add this line: export PATH=$PATH:/usr/local/bin

Q4. What is the syntax for setting an environment variable using the set command in Linux?
A4. The syntax for setting an environment variable using the set command in Linux is to use the following syntax: set VAR_NAME=value.

Q5. How do you set environment variables for a systemd service in Linux?
A5. To set environment variables for a systemd service in Linux, we can use the Environment directive in the service unit file. For example, we can add this line to set the MY_VAR environment variable: Environment=MY_VAR=helloworld

Tag

LinuxEnvars

As a seasoned software engineer, I bring over 7 years of experience in designing, developing, and supporting Payment Technology, Enterprise Cloud applications, and Web technologies. My versatile skill set allows me to adapt quickly to new technologies and environments, ensuring that I meet client requirements with efficiency and precision. I am passionate about leveraging technology to create a positive impact on the world around us. I believe in exploring and implementing innovative solutions that can enhance user experiences and simplify complex systems. In my previous roles, I have gained expertise in various areas of software development, including application design, coding, testing, and deployment. I am skilled in various programming languages such as Java, Python, and JavaScript and have experience working with various databases such as MySQL, MongoDB, and Oracle.
Posts created 3251

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top