Environment variables are an important part of Linux and Bash scripting. They are used to store system-wide or user-specific values that can be accessed by commands, shells, applications, and scripts. If you are learning Bash scripting, understanding environment variables will help you write better automation scripts and manage Linux systems more effectively.
Environment variables are commonly used to store paths, usernames, language settings, configuration values, API keys, and tool settings. In cybersecurity and Linux administration, they are useful for automation, custom tool paths, temporary settings, and script configuration.
An environment variable is a named value available to the shell and its child processes. For example, Linux uses variables like HOME, USER, PATH, and SHELL.
You can view some common environment variables using:
echo $HOMEecho $USERecho $SHELLecho $PATH
Example output:
/home/kalikali/bin/bash/usr/local/bin:/usr/bin:/bin
Here, HOME shows the user’s home directory, while PATH tells Linux where to search for executable commands.
To list all environment variables in Linux, use:
printenv
You can also use:
env
To search for a specific variable, use grep:
printenv | grep HOME
This is useful when checking system settings or debugging Bash scripts.
You can create a temporary environment variable using the export command.
Example:
export SITE_NAME="Kali Linux Tutorials"
Now print the variable:
echo $SITE_NAME
Output:
Kali Linux Tutorials
This variable is temporary. It will work only in the current terminal session. Once you close the terminal, it will be removed.
Create a script:
nano env-example.sh
Add the following code:
#!/bin/bashecho "Website Name: $SITE_NAME"echo "Current User: $USER"echo "Home Directory: $HOME"
Save the file and run it:
chmod +x env-example.sh./env-example.sh
If SITE_NAME was exported earlier, the script can access it.
To make an environment variable permanent for a user, add it to the .bashrc file:
nano ~/.bashrc
Add this line at the bottom:
export SITE_NAME="Kali Linux Tutorials"
Save the file, then reload .bashrc:
source ~/.bashrc
Now check the variable:
echo $SITE_NAME
This variable will be available every time you open a new terminal.
Environment variables in Linux are useful for storing configuration values that can be used by Bash scripts, commands, and applications. You can view them using printenv, create temporary variables using export, and make them permanent by adding them to .bashrc.
For Bash scripting beginners, environment variables are important because they make scripts more flexible and reusable. They are widely used in automation, Linux administration, and cybersecurity workflows.