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.
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:
exportExample Output:
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:
export -pExporting 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:
name () { echo "Hello World"; }Next, export the function:
export -f nameAfterward, invoke the Bash shell and use the function:
bash
nameOutput:
Hello WorldExporting Variables in a Single Step
To assign a value to a variable and export it in a single command, use the following syntax:
export student=DivyaTo verify the value of the exported variable, use the printenv command:
printenv studentOutput:
DivyaConclusion
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!
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.