Environment Variables & export
In this page:
export Makes a Variable Inheritable
By default, a variable assigned in a script is only visible within that script's own shell, not to any programs it launches. export flags the variable so that it becomes part of the environment handed to every child process the script starts from that point on.
Example: export Makes a Variable Inheritable
#!/bin/bash
GREETING="hello from parent"
export GREETING
bash -c 'echo "Child sees: $GREETING"'
Login to try C/C++/Java/PHP code in the editor
Unexported Variables Stay Local
A variable that is never exported is only visible in the current shell and any subshells that inherit its scope directly -- it will not be visible in a separately invoked child process. This distinction is easy to miss because both kinds of variables look identical when you just echo them in the same script.
Warning: The child process prints an empty value for LOCAL_ONLY because it was never exported.
Example: Unexported Variables Stay Local
#!/bin/bash
LOCAL_ONLY="not exported"
bash -c 'echo "Child sees LOCAL_ONLY as: [$LOCAL_ONLY]"'
Login to try C/C++/Java/PHP code in the editor
Common Built-in Environment Variables
Bash and the operating system pre-populate several environment variables at login, such as HOME (the current user's home directory), USER (username), and PATH (the list of directories searched for commands). Scripts frequently read these rather than hard-coding values that differ between systems.
Example: Common Built-in Environment Variables
#!/bin/bash
echo "Home directory: $HOME"
echo "Current user: $USER"
echo "First PATH entry: $(echo "$PATH" | cut -d: -f1)"
Login to try C/C++/Java/PHP code in the editor
Safely Extending PATH
When adding a directory to PATH, always include the existing value ($PATH) in the new assignment, otherwise you overwrite it entirely and the shell can no longer find standard commands. Appending or prepending with a colon separator keeps all previous search locations intact.
Example: Safely Extending PATH
#!/bin/bash
export PATH="/usr/local/mytools:$PATH"
echo "$PATH" | tr ':' '\n' | head -n 3
Login to try C/C++/Java/PHP code in the editor
- Assuming every shell variable is automatically available to child processes/scripts you call; by default variables are local to the current shell and must be
exported to be inherited. - Thinking
exportcopies the variable to the child permanently in both directions; changes a child process makes to an exported variable never flow back up to the parent shell. - Modifying well-known environment variables like
PATHwithout saving the old value first, e.g.PATH=/usr/local/bininstead ofPATH=/usr/local/bin:$PATH, which can wipe out the ability to find other commands.
export VAR=value(orexport VARafter a plain assignment) makes a variable part of the environment passed to child processes.- Environment variables are inherited by child processes at the moment they are started, and changes in the child do not propagate back to the parent.
- Common built-in environment variables include
PATH,HOME,USER, andPWD. envorprintenvlists all currently exported environment variables.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: