Linux Tactic

Mastering Environment Variables in Linux: Adding Modifying and Removing Them

Environment variables are a critical part of the Linux operating system. They play an essential role in the functioning of the bash shell and processes running on Linux.

In this article, we will explore the basics of environment variables in Linux, including their definition, functions, popular examples, and how to add, modify, and remove them.

Understanding Environment Variables in Linux

Environment variables are dynamic values that can affect the behavior of processes running on Linux. Their primary function is to tell the operating system where to find files and how to run programs.

These values are set by users or scripts and are available to all the processes running on the system.

Viewing Environment Variables

You can view all environment variables using either the env or printenv commands. The env command displays the name and value of all environment variables on the system, while the printenv command displays the value of a specific environment variable.

Removing Environment Variables

The unset command is used to remove environment variables from Linux. For example, to remove the UMASK variable, you would run the following command:

“`

unset UMASK

“`

Adding and Modifying Environment Variables

To add or modify an environment variable in Linux, use the export command. For example, to set the PATH variable to include the /usr/local/bin directory, you would run the following command:

“`

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

“`

The above command adds /usr/local/bin to the existing PATH variable.

Popular Environment Variables and Their Functions

Some environment variables are more popular than others. Here is a list of some popular environment variables and their functions:

– SHELL: This variable specifies the default shell for the user.

– HOME: This variable specifies the home directory of the current user. – LANG: This variable specifies the language and localization settings for the system.

– PWD: This variable specifies the current working directory. – LOGNAME: This variable specifies the login name of the current user.

– DESKTOP_SESSION: This variable specifies the desktop environment currently in use. – USER: This variable specifies the username of the current user.

– PATH: This variable specifies the system’s search path for executables.

Conclusion

In conclusion, environment variables are an essential aspect of Linux systems. They play a crucial role in the functioning of the bash shell and other processes running on the system.

In this article, we have covered the definition and functions of environment variables, how to view, add, modify, and remove them. We have also discussed some popular environment variables and their functions.

By understanding the basics of environment variables in Linux, you can better understand how to configure and customize your system.

3) Adding Environment Variables in Linux

Adding environment variables in Linux is a straightforward process. In this section, we will discuss the different methods of adding environment variables in Linux.

Adding Environment Variables for a Script: Export Command

You can add an environment variable to a script by using the export command. For example, to add the FOO environment variable to your script and set its value to “bar,” you would run the following command:

“`

export FOO=”bar”

“`

This command will set the value of the FOO variable to “bar” whenever the script is executed.

Permanent Environment Variables

By default, environment variables are stored in memory and are automatically removed when the system is shut down. If you want to make a variable permanent so that it persists even after a system reboot, you will need to define it in either the .bashrc file for individual users or /etc/bash.bashrc file for the system.

To define a permanent environment variable for an individual user, you will need to edit the .bashrc file in the user’s home directory using a text editor such as nano or vim. For example, to define the FOO environment variable as permanent for the user john, you would run the following commands:

“`

cd ~john

nano .bashrc

“`

This opens the .bashrc file in the nano text editor.

Add the following line to the bottom of the file:

“`

export FOO=”bar”

“`

Save and exit the file by pressing Ctrl+X, followed by Y, and Enter. To define a permanent environment variable for the system, you will need to edit the /etc/bash.bashrc file as the root user.

For example, to define the FOO environment variable as permanent for the entire system, you would run the following command:

“`

sudo nano /etc/bash.bashrc

“`

This will open the bash.bashrc file in the nano text editor. Add the following line to the bottom of the file:

“`

export FOO=”bar”

“`

Save and exit the file by pressing Ctrl+X, followed by Y, and Enter.

4) Removing Duplicate Environment Variable Entries

Over time, your environment variables can accumulate duplicate entries, which can cause problems when executing commands. In this section, we will discuss how to remove duplicate entries from environment variables.

Removing Duplicate Entries

To remove duplicate entries from an environment variable, such as the PATH variable, you will need to use the awk command. For example, to remove duplicate entries from the PATH variable, you would run the following command:

“`

echo $PATH | awk -v RS=: -v ORS=: ‘!($0 in a) {a[$0]; print}’

“`

This command will display the PATH variable without any duplicate entries.

You can set the PATH variable to the output of this command by running the following command:

“`

export PATH=$(echo $PATH | awk -v RS=: -v ORS=: ‘!($0 in a) {a[$0]; print}’)

“`

This command will set the PATH variable to the output of the awk command, which will remove any duplicate entries. In conclusion, adding and removing environment variables in Linux is a critical aspect of configuring and customizing your system.

By understanding how to add, modify and remove environment variables, you can ensure that your system is running optimally and efficiently. The export command allows you to add environment variables to your script, while defining variables in the .bashrc file or /etc/bash.bashrc files enables you to make them permanent.

Furthermore, removing duplicate entries in environment variables can help prevent issues when executing commands. In conclusion, environment variables are an essential aspect of the Linux operating system.

They are dynamic values used to affect the behavior of processes running on Linux. Understanding how to add, modify, and remove environment variables is crucial for configuring and customizing your system.

You can add environment variables to your script using the export command, and for permanent variables, you can edit the .bashrc file or /etc/bash.bashrc files. Additionally, removing duplicate entries from environment variables can help prevent issues when executing commands.

Overall, environment variables are an essential tool for Linux users, and by mastering them, you can optimize your system for peak performance.

Popular Posts