Linux export Command

Linux export Command with Practical Examples

Environment variables are a fundamental part of the operating system that are used by the shell and various processes to store information related to system settings and configurations. The export command in Linux is a built-in command in the Bash shell used to set environment variables. When you start a new shell session, it will not be aware of any variables you’ve set in a previous session because local shell variables are only visible to the shell that created them. When you export a variable, it becomes available to child processes as well. These explanations might not be very clear to you, so in this article, we provide practical examples to teach you how to use the export command in Linux, allowing you to gain a deeper understanding of it. Note that the export command is used in most Linux distributions such as Ubuntu, Debian, CentOS and etc.

export Command Use Cases

  • Setting and configuring environment variables to configure various aspects of the system.
  • Customizing your shell environment using environment variables.
  • Configuring environment variables for authentication and access control for certain programs and services.
  • Making variables available for child processes or other scripts when writing shell scripts.
  • Configuring specific environment variables for the proper functioning of software and libraries.
  • Setting environment variables for system-wide settings.
  • Modifying the behavior of some Linux commands and tools.
  • Configuring environment variables for resource access control.
  • Setting environment variables for debugging and troubleshooting.
  • Passing data to programs through environment variables.

Basic syntax of export command in Ubuntu, Debian, CentOS

To use the features of the export command, you need a Linux system or a Linux VPS. After accessing the Linux terminal (by pressing Ctrl + Alt + T), use the following syntax to set and export environment variables:

export VARIABLE_NAME="value"
  • export: This is the command itself
  • VARIABLE_NAME: Replace the name of the environment variable that you intend to create or modify in this field. (Names of environment variables consist of letters, digits, or underscores.)
  • Value: Specify the value you want to assign for the environment variables. (It can be a string of numbers or any valid data.)

Linux export Command Options

The export command, with its various options, provides the ability to view, delete, or add exported variables. The most common options for the export command are listed here:

  • -p: Lists the names of all exported variables in the current shell.
  • -n: removes the specified variable from the list of exported variables.
  • -f: makes the variable names available as functions.

Now we will teach you practical examples of using linux export command:

View All Exported Variables and Functions

If you run export without further arguments or options, it will provide a list of variables you’ve exported so far:

export

Sample output:

declare  -x HOME="/root"
declare -x  LANG=C.UTF-8"
declare -x  LOGNAME="root"
declare -x  DESKTOP_SESSION="ubuntu"
declare -x  GDMSESSION="ubuntu"

Listing of all Exported Variables in the Current Shell

The following command displays all exported environment variables in the current shell:

export -p

This command has a similar functionality to the export command without options, as both display all the variables used in the current shell.

View Exported Variables

The env or printenv commands are another helpful way to display all environment variables in linux ubuntu, including those you’ve exported:

env

 Or

printenv

The printenv command is used to display exported variables along with their values and to inspect the contents of variables.

Using an Exported Variable in Linux

By using the ‘$‘ symbol followed by the variable name, you can access the value of a variable exported in the shell:

echo $MY_VARIABLE

Output:

Hello, World!

Setting and Exporting a Variable

As mentioned, the export command is used for setting and exporting environment variables. Therefore, by using the export command in ubuntu, you can set an environment variable with a unique name and a specified value:

export MY_VARIABLE="Hello, World!"

By running this command, you set an environment variable called MY_VARIABLE to ‘Hello, World!

Alternatively, you can set a specific value for the variable before exporting it. For example, set the value to 20 for the MY_VARIABLE variable and then export it:

MY_VARIABLE=20

After setting the value for the variable, export it:

export MY_VARIABLE

Then verify the value of the variable using the printenv command:

printenv MY_VARIABLE

Output:

20

Defining the Default Value

The export command also supports assigning a default value to an environment variable. For this purpose, you should add the desired environment variable and its default value to your shell startup files (e.g., .bashrc, .bash_profile, .zshrc). To set a default value for an environment variable, you first need to determine the shell you are using:

echo $SHELL

As a result, you will see the path to your shell such as /bin/bash or /bin/zsh in the previous command’s output.

Then open and edit the configuration file associated with your shell using your favorite editor. for example:

nano ~/.bashrc

Note: ~/.bashrc or ~/.bash_profile are configuration files for the Bash shell.

In the content of the configuration file, add the following line to set the environment variable and the default value:

export MY_VARIABLE="default_value"

Replace “default_value” with the value you want to set as the default.

Save the file and exit.

To make sure the changes are applied, use the following command:

source ~/.bashrc

This command will reload the ~/.bashrc file.

Numerous Variables Export

You can export multiple environment variables simultaneously. As you’ve learned, you can set variables with unique values before exporting them, and then, by typing the variable names one after another, you can export them simultaneously. For example, if we set 3 variables A, B, and C with specific values before exporting, running the export command along with the specified variables can export them simultaneously:

A=15
B=20
C=25
export A B C
printenv A B C

Output:

15
20
25

Removing variables and functions with export command in linux

Using the ‘-n‘ option with the export command allows you to remove variables and functions from the list of exported variables. To do this, use the following command:

Export -n MY_VARIABLE

Replace MY_VARIABLE with the specific variable or function name you want to remove from the list of exported variables. To ensure that you are removing the variable you’ve specified, you can use the export command in combination with the grep command to send the output of the export to grep command and search for the MY_VARIABLE variable:

export | grep MY_VARIABLE

If this command does not provide output, you can make sure to remove the variable MY_VARIABLE from the list of exported variables.

Exporting variable names as a function

To export functions, you can use the export command along with the -f option. The -f option causes the export command to export variable names as functions. To achieve this, first call a function with a specific name. For example, consider a function called ‘name()’:

name () { echo "Hello world"; }

Then export the function by running the following command:

export -f name

Create a new shell session under the parent and invoke the bash shell and then call the specified function:

bash
name

Output:

Hello world

As a result, by launching the new child shell, the specific function is now available.

Exporting Variable for a Single Command

You can export an environment variable for only one command without making it permanent. This is done by prefixing the command with the variable assignment:

MY_VARIABLE="Temporary Value" some_command

By running the previous command, you set MY_VARIABLE for the duration of some_command execution.

Changing the appearance of the terminal

One of the advantages of the export command is that you can customize the appearance of the Linux command prompt, including modifying the prompt, text colors, and other visual aspects. You can achieve this by configuring environment variables related to terminal settings or shell. Modifying the PS1 environment variable in your shell’s configuration file changes the prompt’s appearance ( text displayed before your command input).

# For Bash
export PS1="\u@\h \w $ "
# For Zsh
export PS1="%n@%m %~ % "

For example, you can change the text color using ANSI escape codes and export a variable to set the text color and background color for your prompt:

# Change text color to red and background color to green in Bash
export PS1="\e[31;42m\u@\h \w $ \e[0m"

According to your shell and terminal emulator, you can customize other aspects such as text size, fonts, and transparency through their settings.

Environment Variables in Shell Scripts

Export allows users to make variables available for child processes or scripts called from the main script when writing shell scripts. Suppose on your system, the script myscript.sh exports a variable to export the variable MY_VARIABLE to another_script.sh, use the following command:

# myscript.sh
export MY_VARIABLE="Script Variable"
./another_script.sh

Note: Keep in mind that exported environment variables do not persist between shell sessions, but you can make them available to child processes. Add the export commands to your shell’s profile configuration files to have the variables available in every new terminal session you start.

Conclusion

In this article, you have learned how to use the export command in Linux to set and export environment variables and functions. We hope that by trying this command, your experience in using Linux will improve, and you’ll enjoy the features of the export command. If you have any questions about the export command, feel free to ask us in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked.