Showing posts with label Shell. Show all posts
Showing posts with label Shell. Show all posts

Wednesday, June 3, 2026

How Exports and Evironment Variables in Linux work

Environment variables play a crucial role in how Linux operates. Learn how to use them effectively.
Photo by Abolfazl Pahlavan on Unsplash

Environment variables play a crucial role in how Linux operates, influencing shell behavior, scripts, and applications.

Understanding these concepts can significantly enhance your usage and efficiency in a Linux environment. Let's see how.

What are Environment Variables?

Environment variables (env vars) are dynamic values that affect the processes running on a computer. They provide essential information to both the system and applications about the environment in which they are executing.

Common env vars

Frequent environment variables are listed below. These variables can be used by applications and scripts to behave differently based on the environment settings.

$PATH

This specifies the directories where executable programs are located. The system searches these directories to find commands.

$HOME

Represents the current user's home directory.

$SHELL

Represents the current user's shell.

Examples

# Print the current user's home directory
$ echo $HOME

# Print the name of the currently logged-in user
$ echo $USER

# Print the path to the current shell program
$ echo $SHELL

Setting variables

You can also set environment variables in the shell using the export command. Here's how it works.

To set a variable, simply type:

$ VARIABLE_NAME=value

For example, to set a variable named `MY_VAR`:

$ MY_VAR=123

However, this variable will only be available in the current shell session.

Exporting variables

To make a variable available to child processes, use the `export` command:

$ export VARIABLE_NAME

If we want `MY_VAR` to be available to all child processes, we would do:

$ export MY_VAR

Combining Setting and Exporting

You can also combine both actions: 

$ export MY_VAR=123

Viewing Environment Variables

To see all currently set environment variables, you can use the `env` command or simply type:

$ printenv

To display a single variable, use:

$ echo $VARIABLE_NAME

For example, the command below will show you the directories set in the `PATH` variable:

$ echo $PATH

Unsetting Environment Variables

If you need to remove an environment variable, the `unset` command can be used:

$ unset VARIABLE_NAME

For example, to unset `MY_VAR`:

$ unset MY_VAR

Practical Uses of Environment Variables

Environment variables are handy in various scenarios, let's discuss some of them.

Configuration

Many applications use environment variables for configuration settings. For instance, databases often utilize `DB_HOST`, `DB_USER`, and `DB_PASS` to connect.

Scripts

Writing scripts that depend on which user ran them can use the `USER` variable to customize behavior accordingly.

Software development, DevOps & CI/CD pipelines

When developing software, environment variables often store API keys, secrets, or other sensitive information to keep them outside the source code.

Conclusion

Understanding exports and environment variables in Linux is vital for maximizing system performance and efficiency.

Environment vars allow users to customize their working environment, improve automation in scripts, and ensure that applications can access the configuration settings they need to run correctly.

Mastering these concepts can help streamline your Linux experience and unlock new levels of productivity.

Featured Article

How Exports and Evironment Variables in Linux work

Environment variables play a crucial role in how Linux operates. Learn how to use them effectively. Photo by Abolfazl Pahlavan on Unspla...