Setting Environment Variables in Linux
Setting environment variables in Linux is a crucial step in customizing your system to suit your needs. Environment variables are used to store settings that can be accessed by various programs and scripts running on your system. In this article, we will explore how to set environment variables in Linux, including how to set them globally, locally, and using a shell.
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 **MY_VAR**=value - 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**=value - 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**=value - 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**=value - Save the file and close it.
- Restart the terminal or run
source ~/.bash_profileto apply the changes.
- Open the
Setting Environment Variables Using a Shell
To set environment variables using a shell, you can use the export command. Here’s how to do it:
- Using
exportcommand:- Open a terminal and type
export MY_VAR=value - Press Enter to apply the changes.
- You can also use
export MY_VAR=valueto set multiple variables at once.
- Open a terminal and type
Common Environment Variables
Here are some common environment variables that you should be aware of:
- PATH: This variable stores the directories where the system searches for executable files.
- HOME: This variable stores the user’s home directory.
- USER: This variable stores the user’s username.
- SHELL: This variable stores the shell that the system uses to execute commands.
Tips and Tricks
Here are some tips and tricks to help you set environment variables in Linux:
- Use
exportcommand: Theexportcommand is used to set environment variables. It is the most common way to set environment variables in Linux. - Use
sourcecommand: Thesourcecommand is used to apply environment variables that are set in a shell file. - Use
exportcommand with multiple variables: You can use theexportcommand with multiple variables at once to set multiple environment variables at once. - Use
exportcommand with=operator: You can use the=operator to set environment variables. For example,export MY_VAR=valuesets the variableMY_VARto the valuevalue.
Conclusion
Setting environment variables in Linux is a crucial step in customizing your system to suit your needs. By following the steps outlined in this article, you can set environment variables globally, locally, and using a shell. Remember to use the export command to set environment variables, and use the source command to apply them. With these tips and tricks, you can set environment variables in Linux with ease.
Table: Environment Variables in Linux
| Variable | Description | Usage |
|---|---|---|
| PATH | Stores the directories where the system searches for executable files | export PATH=$PATH:/usr/bin |
| HOME | Stores the user’s home directory | export HOME=/home/user |
| USER | Stores the user’s username | export USER=JohnDoe |
| SHELL | Stores the shell that the system uses to execute commands | export SHELL=bash |
| MY_VAR | Stores a variable value | export MY_VAR=value |
Example Use Cases
- Setting environment variables for a specific user: You can set environment variables for a specific user by using the
exportcommand with theUSERvariable. For example:export USER=JohnDoesets the variableUSERto the valueJohnDoe. - Setting environment variables for a specific directory: You can set environment variables for a specific directory by using the
exportcommand with thePATHvariable. For example:export PATH=/usr/bin:/binsets the variablePATHto the value/usr/bin:/bin. - Setting environment variables for a specific shell: You can set environment variables for a specific shell by using the
exportcommand with theSHELLvariable. For example:export SHELL=bashsets the variableSHELLto the valuebash.
