Fundamentals 6 min read

Master the Linux export Command: Beginner’s Guide to Environment Variables

This article explains how to use the Linux export command to set and view environment variables, demonstrates common options such as listing all exports, exporting specific variables, exporting functions, and shows how to persist settings in shell configuration files, providing practical examples for beginners.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master the Linux export Command: Beginner’s Guide to Environment Variables

The export command in Bash marks variables so that they are passed to child processes, allowing administrators to configure environment parameters without restarting the session.

Displaying All Exported Variables

Running export without arguments lists every exported variable and its value.

linuxmi@linuxmi:~$ export
export output
export output

Finding a Specific Variable

You can pipe the output to grep to locate a particular variable.

linuxmi@linuxmi:~$ export LINUXMI="Welcome to linuxmi.com"
linuxmi@linuxmi:~$ export | grep -i linuxmi
grep example
grep example

Displaying Current Shell Exported Variables

The -p flag prints the list of exported variables in a reusable format.

linuxmi@linuxmi:~$ export -p
export -p output
export -p output

Exporting a New Variable

Assign a value and export it in one step, then verify with grep.

linuxmi@linuxmi:~$ export EDITOR=/usr/bin/gedit
linuxmi@linuxmi:~$ export | grep -i EDITOR
export EDITOR example
export EDITOR example

Exporting a Function

Use the -f option to export a shell function, making it available to subshells.

linuxmi@linuxmi:~$ test () { echo "Test Function"; }
linuxmi@linuxmi:~$ export -f test
linuxmi@linuxmi:~$ bash
Test Function
linuxmi@linuxmi:~$ test
Test Function
export function example
export function example

Persisting Variables Across Sessions

Append an export statement to .bashrc (or another profile file) to set the variable each time a new shell starts.

linuxmi@linuxmi:~$ echo export PATH="/snap/bin/lxd:$PATH" >> .bashrc

After editing, the new PATH will be applied on the next login or when the profile is sourced.

Conclusion

The export command is a simple yet powerful tool for configuring environment parameters in Linux. By mastering its basic options—listing variables, exporting specific values, exporting functions, and persisting settings—you can efficiently manage your shell environment.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShell scriptingExportEnvironment Variables
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.