Export A Variable
Part of the Fundamentals section of Coddy's Terminal journey — lesson 63 of 82.
When you set a variable like MY_NAME="Alice", it exists only in your current shell. If you run a script or start another program, that variable won't be available to it. This is where export comes in.
The export command makes a variable available to any child processes, meaning programs or scripts launched from your current shell can access it:
export MY_NAME="Alice"You can also export an existing variable:
MY_NAME="Alice"
export MY_NAMEBoth approaches achieve the same result. The variable becomes part of the environment that gets passed to any command you run afterward.
To see the difference, consider running a script that tries to use $MY_NAME.
Without export, the script sees nothing. With export, the script can access the value.
This is why system variables like PATH and HOME are exported by default. They need to be available to every program you run.
You can verify a variable is exported by checking if it appears in the env output:
env | grep MY_NAMEExported variables still disappear when you close the terminal. The next lesson covers how to make variables persist across sessions.
Challenge
EasyExport a variable called PROJECT with the value terminal-course, then verify it was exported by filtering the env output to show only your new variable.
You can do this in a single line using export to set and export the variable at once, then pipe env through grep to find it.
Your output should be:
PROJECT=terminal-courseHint: First run
export PROJECT="terminal-course", then useenv | grep PROJECTto verify it appears in the environment.
Cheat sheet
The export command makes a variable available to child processes (programs or scripts launched from your current shell):
export MY_NAME="Alice"You can also export an existing variable:
MY_NAME="Alice"
export MY_NAMEWithout export, variables exist only in the current shell and are not accessible to scripts or programs you run.
To verify a variable is exported, check if it appears in the env output:
env | grep MY_NAMEExported variables disappear when you close the terminal.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline12Environment
Environment VariablesView All VariablesThe PATH VariableSetting A VariableExport A VariableThe Profile FileRecap - Custom Environment