Setting Environment Variables in Linux
Setting environment variables in Linux is a crucial step in customizing the system to suit your needs. Environment variables are used to store settings that can be accessed by various programs and scripts. In this article, we will explore how to set environment variables in Linux, including how to set them globally, locally, and using the export command.
What are Environment Variables?
Environment variables are variables that are stored in the system’s environment, which is a collection of settings that are used by programs and scripts. These variables can be used to customize the system, such as setting the default text editor, the default shell, or the default language.
Setting Environment Variables Globally
To set environment variables globally, you need to add them to the system’s ~/.bashrc or ~/.bash_profile file. Here’s how to do it:
- Using
~/.bashrcfile:- Open the
~/.bashrcfile in a text editor. - Add the following line at the end of the file:
export PATH=$PATH:/usr/local/bin - Save the file and close it.
- Restart the terminal or run
source ~/.bashrcto apply the changes.
- Open the
- Using
~/.bash_profilefile:- Open the
~/.bash_profilefile in a text editor. - Add the following line at the end of the file:
export PATH=$PATH:/usr/local/bin - Save the file and close it.
- Restart the terminal or run
source ~/.bash_profileto apply the changes.
- Open the
Setting Environment Variables Locally
To set environment variables locally, you need to add them to the system’s ~/.bashrc or ~/.bash_profile file. Here’s how to do it:
- Using
~/.bashrcfile:- Open the
~/.bashrcfile in a text editor. - Add the following line at the end of the file:
export MY_VAR="Hello World" - Save the file and close it.
- Restart the terminal or run
source ~/.bashrcto apply the changes.
- Open the
- Using
~/.bash_profilefile:- Open the
~/.bash_profilefile in a text editor. - Add the following line at the end of the file:
export MY_VAR="Hello World" - Save the file and close it.
- Restart the terminal or run
source ~/.bash_profileto apply the changes.
- Open the
Using the export Command
The export command is used to set environment variables. Here’s how to use it:
- Setting environment variables using
exportcommand:- Open the terminal and type
export MY_VAR="Hello World" - Press Enter to apply the changes.
- You can also use
export MY_VAR="Hello World"to set the variable without adding it to the~/.bashrcor~/.bash_profilefile.
- Open the terminal and type
Using the unset Command
The unset command is used to remove environment variables. Here’s how to use it:
- Removing environment variables using
unsetcommand:- Open the terminal and type
unset MY_VAR - Press Enter to apply the changes.
- Open the terminal and type
Using Environment Variables in Scripts
Environment variables can be used in scripts to customize the system. Here’s how to use them:
- Using environment variables in a script:
- Create a new file with a
.shextension, for example,my_script.sh. - Add the following line at the beginning of the file:
export MY_VAR="Hello World" - Add the following lines inside the file:
echo "Hello World" - Save the file and make it executable with
chmod +x my_script.sh. - Run the script with
./my_script.sh.
- Create a new file with a
Best Practices for Setting Environment Variables
Here are some best practices for setting environment variables:
- Use environment variables sparingly: Environment variables should be used sparingly to avoid cluttering the system.
- Use environment variables consistently: Use environment variables consistently throughout the system to avoid confusion.
- Use environment variables in scripts: Use environment variables in scripts to customize the system.
- Use environment variables in configuration files: Use environment variables in configuration files to customize the system.
Conclusion
Setting environment variables in Linux is a crucial step in customizing the system to suit your needs. By following the steps outlined in this article, you can set environment variables globally, locally, and using the export command. Additionally, you can use environment variables in scripts to customize the system. Remember to use environment variables sparingly, consistently, and in scripts to avoid cluttering the system.
