How to Export Command in Linux

Yahya Irmak Feb 02, 2024
How to Export Command in Linux

Linux terminal has many environment variables that contain necessary information about the system. Also, applications may need some environment variables to execute.

This article will define an environment variable with the export command in Linux.

the Variable With the export Command in Linux

The export command is used to create environment variables. You can get the current environment variables with the env command in the Linux terminal.

env

Output:

env command

As seen in the command output above, much information such as path, session, time is stored as environment variables. However, these variables are not always enough.

Some values that applications need to use while running can be obtained from environment variables. This value may be an API key, username, and perhaps password.

We use the export command to define these variables. Example usage is as follows.

export API_KEY=test12345

With this command, API_KEY is now an environment variable. When we use the env command again, we can see that this variable is also in the list.

export api key

This variable is now accessible and usable from the current terminal and its child processes.

Nevertheless, it cannot be accessed from the parent process.

/bin/sh
echo $API_KEY

Output:

access variable from child process

Note
this variable is not permanent and is deleted from the environment variables when the current terminal is closed.

To create persistent environment variables, add them to a file like .bashrc. Also, this command is not the same as the API_KEY=test12345 command.

The variable is not passed to child processes when the export command is not used. It can only operate in the current terminal.

Author: Yahya Irmak
Yahya Irmak avatar Yahya Irmak avatar

Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.

LinkedIn

Related Article - Linux Command