The Linux operating system is highly customizable, and one of its powerful features is the use of environment variables. Environment variables play a crucial role in configuring and controlling various aspects of the Linux system. In this lesson, we will explore what environment variables are and how they work in the Linux environment.
What are Environment Variables?
Environment variables are dynamic values stored within the operating system that can affect the behavior of processes and applications running on a Linux system. They provide a way to configure settings, define user preferences, and share information between different programs.
How are Environment Variables Defined? In Linux, environment variables are represented as key-value pairs. The variable name acts as the key, while its associated value represents the data stored within it. These variables can be set at different scopes: globally for all users or specific to individual users.
Common Environment Variables:
There are some very commonly used environment variables which you can find in any UNIX environment.
PATH
- The PATH variable specifies which directories contain executable files.
- When a command is entered in the terminal, the shell searches through these directories to find the corresponding executable file.
HOME
- The HOME variable indicates the user’s home directory.
- This variable helps programs reference configuration files or store user-specific data.
SHELL
- This stores the the path of the current user’s shell. This can be
/bin/bashor/bin/zshor any other shell.
- This stores the the path of the current user’s shell. This can be
PS1
- The PS1 variable controls the prompt display in the terminal.
- By modifying this variable’s value, users can customize their shell prompt appearance.
PWD
- This prints the current working directory
UID
- User’s unique identifier - This is usually a numeric ID for the current user.
HOSTNAME
- Device’s hostname on the network
LD_LIBRARY_PATH
- The
LD_LIBRARY_PATHvariable specifies additional paths for shared libraries. - It tells programs where to find required libraries during runtime.
- The
Working with Environment Variables:
We can view environment variables in at least two different ways.
Viewing Environment Variables
- If we type
echo $followed by two tabs, we can see all environment variables available. Two Tabs are used for auto-completion in bash session. If we writecdfollowed by two tabs, it will try to list the available directories it the current directory. - We can use
printenvorenvcommand to dispaly the list of currently defined environment variables and their values.
Setting and Updating Environment Variables
Setting Variables:
- To set an environment variable, use the
exportcommand followed by the variable name and its value. - For example:
export ENV_VARIABLE=my_value
- To set an environment variable, use the
Modifying Variables:
- Use the
exportcommand again with a new value to modify an existing variable. - For example:
export ENV_VARIABLE=new_value
- Use the
To make variables persist across terminal sessions or reboots, define them in profile files like ~/.bashrc or ~/.bash_profile.
Understanding Linux environment variables is essential for system administrators, developers, and power users alike. These variables play a significant role in customizing and configuring various aspects of the Linux system. By mastering environment variables, you can wield greater control over your Linux environment and enhance your productivity.


Comments