This article introduces the use of export, exec, source, and ..
1. The Login Shell on Linux
The Shell that starts when you log in to a Linux system is a process. Running another Shell script from that process may start a new process.
Variables created in the new Shell cannot be read directly by its parent process, the current Shell. This is where export and source are useful.
source executes a script in the current process instead of starting a new one.
After changing environment variables, for example by editing ~/.bashrc, run source ~/.bashrc to apply the variables defined there.
Inside ~/.bashrc, you can add an environment variable with export VAR=value.
The source command can also be written as a single dot: ..
2. The exec Command
What, then, does exec do? It is a separate Shell command.
After exec runs, the current Shell is replaced by the specified command.
Running a script with sh or bash starts a separate execution environment for that script.
Running it with source or . does not create a new environment; the code executes from top to bottom in the current one.
Searching the web for “Linux Shell exec” often brings up documentation for the exec family of functions. Those functions are APIs used in languages such as C and C++, whereas this article discusses the Linux Shell command, although the two are closely related.
3. export
The export command marks a variable so that subsequently started child processes can access it.
4. Summary
This article covered three Shell commands: source or . executes commands in the current process; exec replaces the current process with another command; and export makes variables available to child processes.