Environment variables are a crucial aspect of any operating system, allowing users to store and manage system-wide or user-specific data, and configure applications or services. In Ubuntu, environment variables are used to customize the behavior of the shell, define paths, and store settings for various software. In this comprehensive guide, we will explore how to set up and manage environment variables on Ubuntu effectively.
1. Understanding Environment Variables
Environment variables are key-value pairs that can be accessed and modified by various applications and services running on your system. They store data such as:
- System and user paths
- API keys and credentials
- Software configurations
- Default settings for shell and commands
2. Viewing Environment Variables
To view the current environment variables, you can use the `printenv` command followed by the variable name. To view all the environment variables, simply run:
printenv
or
env
3. Setting Environment Variables Temporarily
You can set an environment variable temporarily by using the export command in the terminal. The variable will only be available for the current session and will be lost when the terminal is closed.
To set the environment variable, run:
export MY_VARIABLE="My Value"
To verify that the variable has been set, run:
printenv MY_VARIABLE
4. Setting Environment Variables Permanently
To set an environment variable permanently, you need to add the export command to a system-wide or user-specific configuration file.
4.1. System-wide Environment Variables
For system-wide environment variables, edit the /etc/environment file. This file contains a list of key-value pairs, one per line:
sudo nano /etc/environment
Add your environment variable at the end of the file:
1 | MY_VARIABLE="My Value" |
Save and exit the file. For the changes to take effect, either reboot the system or run:
source /etc/environment
4.2 User-specific Environment Variables
For user-specific environment variables, you can edit the `~/.profile` or `~/.bashrc` file. In this guide, we will use the `~/.profile` file:
nano ~/.profile
Add your environment variable at the end of the file:
1 | export MY_VARIABLE="My Value" |
Save and exit the file. To apply the changes immediately, run:
source ~/.profile
5. Managing Environment Variables using .bashrc and .profile
The `~/.bashrc` and `~/.profile` files are commonly used to manage user-specific environment variables and configurations. The main difference between the two files is their purpose and when they are executed:
- ~/.bashrc is executed for interactive non-login shells, such as when you open a new terminal window.
- ~/.profile is executed for login shells, such as when you log in via SSH or the TTY.
In most cases, adding environment variables to `~/.profile` is recommended, as it ensures the variables are available for both login and non-login shells. However, if you need a variable to be set only for interactive shells, use `~/.bashrc`.
6. Setting Environment Variables using /etc/profile.d/*.sh Files
You can also create a shell script under the /etc/profile.d directory. During the user login /etc/profile script is executed. Tha also executed all the shell scripts (files with .sh extension) under /etc/profile.d directory.
Let’s create a shell script /etc/profile.d/custom-env.sh and set the environment variables using export command.
sudo vim /etc/profile.d/custom-env.sh
Set the environment variables like:
/etc/profile.d/custom-env.shexport JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export JRE_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export MY_ENV="value1"
The next time the user logged in will automatically set the environment variables. You can print the value of the environment variable using the echo command.
echo $MY_ENV
value1
Conclusion
Understanding and managing environment variables is essential for customizing and configuring your Ubuntu system. By following this guide, you have learned the ins and outs of setting up and managing environment variables on Ubuntu, both temporarily and permanently, as well as using the `.bashrc` and `.profile` files to manage user-specific variables.
In conclusion, environment variables play a vital role in the efficient functioning of your Ubuntu system. They enable you to store sensitive data securely, define system and user paths, and configure various applications or services. By mastering environment variables, you can optimize your system’s performance, streamline your workflow, and ensure a more organized and well-configured environment for your applications and services.
Remember to always be cautious when modifying environment variables, as incorrect configurations may lead to system instability or malfunctioning applications. Always backup important files and configurations before making changes, and if in doubt, consult the official documentation or seek assistance from the Ubuntu community.