Environment variables are named values that are stored in the system’s environment. They are accessible to all processes running on the system, and they can be used to pass information between different parts of the operating system and applications. For example, the PATH
variable contains a list of directories where the shell looks for executable files.
LANG
variable, which determines the language settings for the system.To view all the environment variables in the current shell session, you can use the env
or printenv
commands.
# Using env command
env
# Using printenv command
printenv
To view a specific environment variable, you can pass its name as an argument to the printenv
command.
printenv PATH
VARIABLE_NAME=value
For example, to set a variable named MY_VAR
with the value hello
, you can run:
MY_VAR=hello
printenv MY_VAR
.bashrc
or .bash_profile
.# Open the .bashrc file in a text editor
nano ~/.bashrc
# Add the following line to the file
export MY_VAR=hello
# Save the file and exit the text editor
# Reload the .bashrc file
source ~/.bashrc
# Check if the variable is set
printenv MY_VAR
To unset an environment variable, you can use the unset
command.
unset MY_VAR
printenv MY_VAR
PATH
VariableThe PATH
variable is one of the most important environment variables in Linux. It contains a list of directories where the shell looks for executable files. You can add a new directory to the PATH
variable to make the commands in that directory accessible from the command line.
# Add a new directory to the PATH variable temporarily
export PATH=$PATH:/new/directory/path
# Add a new directory to the PATH variable permanently
echo 'export PATH=$PATH:/new/directory/path' >> ~/.bashrc
source ~/.bashrc
HOME
VariableThe HOME
variable stores the path to the user’s home directory. It is used by many applications to locate user - specific files and settings. You can use the HOME
variable in your scripts or commands to refer to the home directory.
# Create a file in the home directory
touch $HOME/myfile.txt
Environment variables are a powerful feature of the Linux command - line environment. They allow you to customize your shell environment, simplify commands, and enhance system security. By understanding the fundamental concepts, usage methods, common practices, and best practices of environment variables, you can become more efficient and effective in using the Linux command line.