0
0
Linux CLIscripting~5 mins

export command in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to make a variable available to other programs or scripts you run from your shell. The export command lets you do this by marking variables so they can be used by child processes.
When you want a script to access a variable you set in your current shell session.
When you need to set environment variables like PATH or HOME for programs you start.
When you want to temporarily change a variable for a program you launch without changing it globally.
When you want to share configuration values between multiple commands run from the same shell.
When you want to make sure a variable is available to all scripts called from your shell.
Commands
This sets a variable named MY_VAR with the value 'hello' in the current shell session only.
Terminal
MY_VAR=hello
Expected OutputExpected
No output (command runs silently)
This prints the value of MY_VAR to confirm it is set in the current shell.
Terminal
echo $MY_VAR
Expected OutputExpected
hello
This marks MY_VAR to be exported, so any program or script started from this shell can access it.
Terminal
export MY_VAR
Expected OutputExpected
No output (command runs silently)
This runs a new shell and prints MY_VAR to show that the exported variable is available in child processes.
Terminal
bash -c 'echo $MY_VAR'
Expected OutputExpected
hello
Key Concept

If you remember nothing else from this pattern, remember: export makes a shell variable available to all programs started from that shell.

Common Mistakes
Setting a variable without export and expecting child processes to see it.
Variables set without export are only visible in the current shell, not in programs started from it.
Use export to mark variables you want child processes to access.
Using export without setting a variable first.
Exporting a variable that is not set does nothing useful and may cause confusion.
Set the variable first, then export it.
Summary
Set a variable in the shell using VAR=value.
Use export VAR to make the variable available to child processes.
Verify by running a child shell or program that prints the variable.