export command in Linux CLI - Time & Space Complexity
We want to understand how the time it takes to run the export command changes as we use it more in scripts or the shell.
Specifically, how does the cost grow when exporting many variables?
Analyze the time complexity of the following code snippet.
for var in VAR1 VAR2 VAR3 VAR4 VAR5; do
export $var=value
done
This code exports five environment variables one by one in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs the
exportcommand once per variable. - How many times: The loop runs once for each variable to export.
Each new variable adds one more export command execution.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 export commands |
| 100 | 100 export commands |
| 1000 | 1000 export commands |
Pattern observation: The total work grows directly with the number of variables exported.
Time Complexity: O(n)
This means the time to export variables grows in a straight line as you add more variables.
[X] Wrong: "Exporting many variables happens instantly no matter how many."
[OK] Correct: Each export command takes some time, so more variables mean more commands and more time.
Understanding how simple shell commands scale helps you write efficient scripts and shows you think about performance even in small tasks.
"What if we export all variables in one command instead of a loop? How would the time complexity change?"
Practice
export command do in a Linux shell?Solution
Step 1: Understand shell variables and environment
Shell variables exist only in the current shell session unless exported.Step 2: Role of
Theexportexportcommand marks a variable to be passed to child processes as environment variables.Final Answer:
Makes a shell variable available to child processes -> Option AQuick Check:
export = share variable with child processes [OK]
- Thinking export deletes variables
- Confusing export with file commands
- Assuming export changes directories
PATH with value /usr/local/bin?Solution
Step 1: Recall export syntax
The correct syntax isexport VARIABLE=VALUEwithout spaces around the equal sign.Step 2: Check each option
export PATH=/usr/local/bin matches the correct syntax. Others have invalid characters or spaces.Final Answer:
export PATH=/usr/local/bin -> Option BQuick Check:
export VAR=VALUE no spaces [OK]
- Adding spaces around equal sign
- Using wrong symbols like : or ==
- Placing export after assignment
MYVAR="hello" echo $MYVAR export MYVAR bash -c 'echo $MYVAR'
Solution
Step 1: Understand variable assignment and export
First, MYVAR is set to "hello" and printed, so first echo outputs "hello". Then MYVAR is exported.Step 2: Child shell access
Thebash -c 'echo $MYVAR'runs a new shell which inherits exported variables, so it prints "hello" again.Final Answer:
hello hello -> Option DQuick Check:
Exported variable visible in child shell [OK]
- Assuming unexported vars appear in child shells
- Confusing echo outputs with command prompts
- Ignoring export effect on subshells
export MYVAR MYVAR="world" echo $MYVAR
Solution
Step 1: Analyze export before assignment
Exporting MYVAR before assigning means MYVAR is empty when exported.Step 2: Effect on variable value
Assigning MYVAR after export does not update the exported environment variable for child processes.Final Answer:
MYVAR is exported before assignment, so child shells get empty value -> Option CQuick Check:
Export before assignment exports empty value [OK]
- Exporting before assigning value
- Thinking export syntax is wrong
- Assuming echo needs quotes always
CONFIG_PATH to /etc/config and ensure it is available to all scripts run from your current shell session and its children. Which sequence of commands achieves this correctly?Solution
Step 1: Assign variable before export
AssigningCONFIG_PATH=/etc/configsets the value in the current shell.Step 2: Export variable after assignment
Runningexport CONFIG_PATHmarks it for child processes with the assigned value.Step 3: Check other options
export CONFIG_PATH=/etc/config works but is less clear; export CONFIG_PATH\nCONFIG_PATH=/etc/config exports before assignment (wrong); CONFIG_PATH=/etc/config does not export.Final Answer:
CONFIG_PATH=/etc/config export CONFIG_PATH -> Option AQuick Check:
Assign then export to share variable [OK]
- Exporting before assigning value
- Not exporting variable at all
- Assuming single export with assignment is always best
Ready to prove it?
Pass the test and this pattern moves unknown
