0
0
Linux CLIscripting~10 mins

export command in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - export command
Start shell session
Define variable
Use export command
Variable added to environment
Child processes inherit variable
End
The export command marks a shell variable to be passed to child processes, making it part of the environment.
Execution Sample
Linux CLI
MYVAR=hello
export MYVAR
bash -c 'echo $MYVAR'
Defines a variable, exports it, then runs a child shell that prints the variable's value.
Execution Table
StepActionVariable StateOutput
1Define MYVAR=helloMYVAR=hello (shell only)
2Run export MYVARMYVAR=hello (environment)
3Run child shell: bash -c 'echo $MYVAR'Child shell inherits MYVAR=hellohello
4EndNo change
💡 Child shell prints 'hello' because MYVAR was exported to environment.
Variable Tracker
VariableStartAfter exportIn child shell
MYVARundefinedhello (exported)hello
Key Moments - 2 Insights
Why does the child shell print 'hello' only after export?
Because before export, MYVAR is only a shell variable, not in the environment. Export adds it to the environment so child shells can see it, as shown in execution_table step 3.
What happens if you define a variable but do not export it?
The variable stays local to the current shell and is not visible to child processes, so commands like 'bash -c' won't see it (see execution_table step 1 vs 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of MYVAR in the child shell?
Ahello
Bundefined
Cempty string
DMYVAR
💡 Hint
Check execution_table row 3 under Output and Variable State columns.
At which step does MYVAR become part of the environment?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table step 2 where export is run.
If you remove the export command, what will the child shell output be?
Ahello
BMYVAR
Cempty string
Derror
💡 Hint
Refer to key_moments about variable visibility without export.
Concept Snapshot
export command:
- Marks a shell variable for environment export
- Makes variable visible to child processes
- Syntax: export VAR_NAME
- Without export, variable is local to current shell
- Useful for passing config to scripts or subshells
Full Transcript
The export command in Linux shell is used to make a variable part of the environment. When you define a variable like MYVAR=hello, it exists only in the current shell. Running export MYVAR adds it to the environment so child processes, like a new bash shell, can access it. This is why the child shell prints 'hello' when running 'bash -c echo $MYVAR'. Without export, the child shell would not see the variable and print nothing. Export is essential for passing variables to scripts or subshells.