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:
Or
To view a specific environment variable, use the echo command:
Common Environment Variables
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:
This variable will only be available in the current session. You can verify it with:
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:
After adding the line, apply the changes by sourcing the file:
Examples
Recommended by LinkedIn
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:
This variable can then be used in scripts or applications that need to know where the configuration file is located:
3. Using Environment Variables in Scripts:
You can use environment variables within shell scripts. For example, create a script my_script.sh:
Make the script executable:
Run the script:
Unsetting Environment Variables
To remove an environment variable, use the unset command:
This will remove the variable from the current session.
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!