Tutorials  /  Linux Basics

Understanding the Export Command in Linux

Ccentron Redaktion · November 2024 ·4 min read ·Linux Basics, Tutorial

The export command is an essential built-in function of the Bash shell in Linux. It is used to mark environment variables and functions so that they are passed to child processes. This guide explains how to use the export command effectively with examples, allowing you to manage variables and functions across your Linux environment.

What Does the Export Command Do?

When you export a variable, it becomes available to all child processes initiated by the shell. This ensures that the variable is included in the environment of those processes, making it a vital tool for scripting and system configuration. Let’s explore some practical examples of using the export command.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Viewing All Exported Variables

To display all currently exported variables in your shell environment, you can run the export command without any arguments. Here’s an example:

Console
export

Example Output:

Code
declare -x HOME="/root"
declare -x LANG="C.UTF-8"
declare -x LOGNAME="root"

Using the -p Flag to View Exported Variables

The -p flag can be used to list all exported variables in a more explicit format. This is particularly helpful when working with scripts or debugging:

Console
export -p

Exporting Functions in Linux

The export command can also be used to export functions. This is useful when you need to share a function with child processes. Here’s how to do it:

First, define the function:

Code
name () { echo "Hello World"; }

Next, export the function:

Console
export -f name

Afterward, invoke the Bash shell and use the function:

Code
bash
name

Output:

Code
Hello World

Exporting Variables in a Single Step

To assign a value to a variable and export it in a single command, use the following syntax:

Console
export student=Divya

To verify the value of the exported variable, use the printenv command:

Code
printenv student

Output:

Code
Divya

Conclusion

The export command is a powerful tool for managing environment variables and functions in Linux. It provides flexibility and control when working with child processes. By mastering this command, you can enhance your scripting and system configuration skills. Try out the examples provided here and see how they can improve your workflow.

Your feedback is always welcome. Feel free to share your experience with the export command in the comments below!

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren