1. Introduction
In Ubuntu and other Linux distributions, environment variables play an essential role in configuring system and application behavior. Environment variables store settings that allow you to customize and optimize the operation of the system and programs. This article provides a step-by-step explanation on how to check, set, delete, and apply environment variables in Ubuntu. Even beginners can follow along, thanks to concrete command examples that offer valuable insights for both system administrators and developers.
2. What Are Environment Variables?
Environment variables are values referenced by systems and programs that store specific configuration data to customize behavior. With environment variables, the same program can operate differently depending on the settings, and system-wide configurations can be modified dynamically.
Practical Uses of Environment Variables
Environment variables are useful in various scenarios, such as:
- PATH: Stores a list of directories where executable files are located. You can run commands without typing their full path.
- HOME: Points to the user’s home directory, enabling access to user-specific settings and files.
- LANG: Manages system language settings and determines the language used for system messages and programs.
3. How to View Environment Variables
Several commands allow you to check environment variables. You can easily confirm currently configured variables in your shell session or inspect specific variable values.
Check a Specific Environment Variable
To view the value of a specific environment variable, use the echo command:
echo $PATHThis command displays the list of directories stored in the PATH variable, showing where executables are searched.
View All Environment Variables
To list all environment variables configured in the current shell, use the env or printenv command.
envThis displays all environment variables and their values. The export -p command is also convenient for checking exported environment variables.
4. How to Set Environment Variables
Setting Temporary Environment Variables
Temporary environment variables can be set using the export command. These variables are cleared when the shell session ends, making them suitable for short-term use.
export MY_VARIABLE="hello"This assigns the value “hello” to the variable MY_VARIABLE. Once the shell is closed, the variable is removed.
Setting Persistent Environment Variables
To configure environment variables permanently, add them to the .bashrc or .profile file. These settings remain active even after system reboot.
- Add to
.bashrc:
echo 'export MY_VARIABLE="hello"' >> ~/.bashrc- Add to
.profile:
echo 'export MY_VARIABLE="hello"' >> ~/.profileTo apply changes, reload the file using the following command:
source ~/.bashrcThis ensures the variable is available the next time a shell starts.

5. How to Remove Environment Variables
Removing Variables with the unset Command
To delete an environment variable, use the unset command:
unset MY_VARIABLEThis removes the variable MY_VARIABLE, making it unavailable for reference.
Removing Variables Using export -n
You can also delete environment variables using the export -n command:
export -n MY_VARIABLEThis disables the specified environment variable in the same manner.
6. Advanced Usage: Practical Applications of Environment Variables
Using Environment Variables in Scripts
By using environment variables in scripts, you can dynamically change behavior. The example below uses the value of MY_VARIABLE to branch logic:
#!/bin/bash
if [[ $MY_VARIABLE == "production" ]]; then
echo "本番環境です"
else
echo "開発環境です"
fiThis approach allows scripts to behave differently depending on the execution environment.
System-Wide Environment Variables
To configure system-wide environment variables, edit the /etc/environment file. Variables set here apply to all users and shells.
sudo nano /etc/environmentAdd a variable as follows:
MY_VARIABLE="hello"The new settings will take effect after reboot.
7. Troubleshooting
When Environment Variables Do Not Take Effect
If environment variables do not apply as expected, manually reload settings using the source command:
source ~/.bashrcAlternatively, restarting the shell (closing and reopening the terminal) also activates new settings.
Variables Disappear After Reboot
If environment variables are lost after reboot, they may not be written correctly in .bashrc or .profile. Ensure the correct syntax and reload them using the source command.
8. Conclusion
Managing environment variables in Ubuntu is essential for efficient system administration and development workflows. This article introduced the basics, including how to check, set, and delete environment variables using concrete commands. Incorporate these practices into your daily operations to streamline system management and enhance productivity.



