
Environment variables in Bash are a crucial part of any Linux or UNIX system. They contain important data and settings for the shell and user sessions. This article will guide you through the process of setting environment variables in Bash.
To set environment variables in Bash, you can use the syntax VARIABLE_NAME=value
to set a variable for the current session only. To make the variable available for new processes as well, use the export
command. To make the variable persistent across sessions, add it to the .bashrc
or .bash_profile
file in your home directory. For system-wide availability, add the variable to the /etc/environment
file.
What are Environment Variables?
Environment variables are dynamic named values that can affect the way running processes behave on a computer. They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP
environment variable to discover a suitable location to store temporary files, or the HOME
variable to find the directory structure owned by the user running the process.
Setting Environment Variables Temporarily
You can set an environment variable in Bash for the current session only. This can be done using the following syntax:
VARIABLE_NAME=value
For instance, if you want to set a variable named TEST_VAR
to Hello World
, you would run:
TEST_VAR="Hello World"
This variable will be available for the duration of the session. However, if you start a new session or open a new terminal, the variable will not be available.
Exporting Environment Variables
If you want to set an environment variable not only for the current session but also for any new processes from the current shell, you can use the export
command:
export VARIABLE_NAME=value
For example:
export TEST_VAR="Hello World"
The export
command marks the variable to be exported to child-processes, which means it will be inherited by them.
Setting Environment Variables Permanently
To make the environment variable persistent across sessions, you can add it to the .bashrc
or .bash_profile
file in your home directory. These files are scripts that are executed when a new terminal session is started.
Open the .bashrc
file with a text editor, such as nano
:
nano ~/.bashrc
At the end of the file, add the export
command:
export TEST_VAR="Hello World"
Save the file and exit. The changes will take effect when you start a new session, or you can source the .bashrc
file to apply the changes immediately:
source ~/.bashrc
Setting System-Wide Environment Variables
To set an environment variable system-wide, which means it will be available to all users and processes, you can add it to the /etc/environment
file.
Open the file with root privileges:
sudo nano /etc/environment
And add your variable in the following format:
TEST_VAR="Hello World"
Save the file and exit. The changes will take effect after a reboot, or you can use the source
command to apply them immediately:
source /etc/environment
Conclusion
Setting environment variables in Bash is a common task that is crucial for managing the behavior of your system and applications. Whether you need to set a variable for a single session, for all sessions of a single user, or system-wide, Bash provides the tools to do it. Remember to replace VARIABLE_NAME
and value
with your actual variable name and value.
By understanding and using environment variables effectively, you can control the system’s behavior, customize your environment, and manage processes efficiently.
You can use the echo
command followed by the variable name enclosed in $
to display its value. For example, to check the value of the TEST_VAR
variable, you would run echo $TEST_VAR
.
Yes, you can have spaces in the value of an environment variable. Just make sure to enclose the value in double quotes when assigning it. For example, TEST_VAR="Hello World"
is a valid assignment.
To unset an environment variable, you can use the unset
command followed by the variable name. For example, to unset the TEST_VAR
variable, you would run unset TEST_VAR
.
You can use the env
command to list all environment variables in Bash. Simply run env
without any arguments, and it will display a list of all variables and their values.
Yes, you can use environment variables in Bash scripts. Simply reference the variable using the $
symbol followed by the variable name. For example, to use the value of the TEST_VAR
variable in a script, you would use $TEST_VAR
.
To apply changes to the environment variables immediately, you can use the source
command followed by the file that contains the variable assignments. For example, to apply changes made in the .bashrc
file, you would run source ~/.bashrc
.