0
0
Linux CLIscripting~10 mins

Environment variables in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Environment variables
Start shell session
Set environment variable
Use variable in commands
Variable accessed by shell or programs
End session or export variable for child processes
This flow shows how environment variables are set, used, and accessed in a shell session.
Execution Sample
Linux CLI
MY_VAR=hello
export MY_VAR
echo $MY_VAR
Sets an environment variable MY_VAR to 'hello', exports it, then prints its value.
Execution Table
StepCommandActionVariable StateOutput
1MY_VAR=helloAssign MY_VAR locallyMY_VAR=hello (local)
2export MY_VARMake MY_VAR available to child processesMY_VAR=hello (exported)
3echo $MY_VARPrint value of MY_VARMY_VAR=hello (exported)hello
4exitEnd shell sessionSession ends
💡 Shell session ends, environment variables cleared unless saved in config files
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
MY_VARundefinedhello (local)hello (exported)hello (exported)session ends
Key Moments - 2 Insights
Why does MY_VAR need to be exported to be available to child processes?
Because local variables are only visible in the current shell. Exporting MY_VAR (see step 2 in execution_table) makes it part of the environment passed to child processes.
What happens if you try to echo $MY_VAR before setting it?
The output will be empty because MY_VAR is undefined. This is shown by the variable state 'undefined' before step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the output of 'echo $MY_VAR'?
Ahello
B$MY_VAR
Cundefined
Derror
💡 Hint
Check the Output column at step 3 in the execution_table.
At which step does MY_VAR become available to child processes?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the Variable State column and the Action column in the execution_table.
If you remove the 'export' command, what will be the output of 'echo $MY_VAR' in a child process?
Ahello
Bempty string
Cerror message
D$MY_VAR
💡 Hint
Refer to the key_moments explanation about exporting variables.
Concept Snapshot
Environment variables store data for shell and programs.
Set with VAR=value, export with 'export VAR' to share with child processes.
Access with $VAR.
Local variables are not passed to child processes.
Exported variables are inherited by child processes.
Full Transcript
Environment variables are named values stored in the shell environment. You can set a variable by typing VAR=value. This variable is local to the current shell session. To make it available to programs or child shells, you must export it using 'export VAR'. Once exported, the variable is passed down to child processes. You can access the value by prefixing the variable name with a dollar sign, like $VAR. If you try to use a variable before setting it, it will be empty. When the shell session ends, the environment variables are cleared unless saved in configuration files.