How To

How To Use The Export Command In Linux Bash

The export command in Linux is used to create environment variables that can be accessed by Bash scripts, commands, and child processes. If you are learning Bash scripting, understanding the export command is very important because it helps you pass values from your terminal to scripts.

In simple words, a normal Bash variable works only inside the current shell. But when you use export, the variable becomes available to other programs or scripts started from that shell. This is useful for automation, Linux administration, development, and cybersecurity scripting.

What Is The Export Command In Linux?

The export command marks a variable as an environment variable. This means the variable can be used by child processes.

Basic syntax:

export VARIABLE_NAME="value"

Example:

export WEBSITE="kalilinuxtutorials.com"

Now you can print the variable:

echo $WEBSITE

Output:

kalilinuxtutorials.com

Difference Between Normal Variable And Exported Variable

A normal Bash variable is available only in the current shell.

Example:

SITE="Kali Linux Tutorials"

Print it:

echo $SITE

This works in the current terminal. But if you run another Bash process or script, it may not be available unless you export it.

Now export the variable:

export SITE="Kali Linux Tutorials"

This makes it available to child processes and scripts.

Using Export Command With Bash Script

Create a Bash script:

nano export-example.sh

Add the following code:

#!/bin/bashecho "Website Name: $SITE"echo "Current User: $USER"echo "Home Directory: $HOME"

Save the file and give execute permission:

chmod +x export-example.sh

Now export the variable:

export SITE="Kali Linux Tutorials"

Run the script:

./export-example.sh

Output:

Website Name: Kali Linux TutorialsCurrent User: kaliHome Directory: /home/kali

This shows that the Bash script can access the exported variable.

How To View Exported Variables

To view all exported environment variables, use:

export

You can also use:

printenv

To check a specific variable:

printenv SITE

Or:

echo $SITE

How To Remove An Exported Variable

To remove a variable, use the unset command:

unset SITE

Now check it again:

echo $SITE

If nothing is displayed, the variable has been removed.

Making Export Variables Permanent

Exported variables are temporary by default. They disappear when you close the terminal. To make them permanent, add them to the .bashrc file:

nano ~/.bashrc

Add this line:

export SITE="Kali Linux Tutorials"

Reload the file:

source ~/.bashrc

Conclusion

The export command in Linux Bash is used to make variables available to scripts and child processes. It is commonly used for setting paths, configuration values, usernames, tool options, and automation variables.

For Bash scripting beginners, learning the export command helps you understand how environment variables work. It also makes your scripts more flexible, reusable, and useful for real-world Linux and cybersecurity tasks.

Cyber Defence

Recent Posts

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

4 days ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

4 days ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

4 days ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

4 days ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

4 days ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

5 days ago