Environment Variables in LINUX
Environment Variables in LINUX

Environment Variables in LINUX

Environment variables in Linux are dynamic values that can affect the behavior of processes running on the system. They are used to pass configuration information to applications and system services. Here’s a detailed explanation of environment variables with examples:


What are Environment Variables?

Environment variables are key-value pairs stored in the operating system's environment. They can influence the way running processes behave on a computer. Commonly used environment variables include PATH, HOME, USER, and SHELL.


Viewing Environment Variables

You can view all the environment variables using the printenv or env command:

Article content
To View all the Environment Variables

Or

Article content
To View all the Environment Variables


To view a specific environment variable, use the echo command:

Article content
To View Specific Environment Variables


Common Environment Variables

  1. PATH: Specifies the directories in which the system looks for executable files.
  2. HOME: The home directory of the current user.
  3. USER: The name of the current user.
  4. SHELL: The path to the current user's shell.


Setting Environment Variables

Environment variables can be set temporarily for the current session or permanently.

Temporarily

To set an environment variable temporarily, use the export command:

Article content
To set an environment variable

This variable will only be available in the current session. You can verify it with:

Article content
To Verify Variable value

Permanently

To set an environment variable permanently, add the export command to your shell configuration file. For example, if you are using bash, add the following line to your ~/.bashrc or ~/.bash_profile file:

Article content
To set an environment variable

After adding the line, apply the changes by sourcing the file:

Article content
Apply changes to set an environment variable

Examples

  1. Adding a Directory to the PATH Variable:

Article content
Adding a Directory

To make this change permanent, add the line to your ~/.bashrc or ~/.bash_profile.


2. Setting a Custom Environment Variable:

Create a custom environment variable MY_APP_CONFIG to store a configuration path:

Article content
Setting a Custom Environment Variable

This variable can then be used in scripts or applications that need to know where the configuration file is located:

Article content


3. Using Environment Variables in Scripts:

You can use environment variables within shell scripts. For example, create a script my_script.sh:

Article content
Using Environment Variables in Scripts

Make the script executable:

Article content

Run the script:

Article content


Unsetting Environment Variables

To remove an environment variable, use the unset command:

Article content
Unsetting Environment Variables

This will remove the variable from the current session.


Article content
Some commonly used ENVs in Linux


Conclusion

Environment variables are a fundamental part of the Linux operating system, providing a way to configure and control the behavior of processes and applications. By understanding and using environment variables, you can customize your Linux environment to better suit your workflow and needs.



Happy Learning!




To view or add a comment, sign in

Others also viewed

Explore content categories