Understanding the 'source' Command in Unix (with examples)
The source
command is a powerful utility used in Unix-like operating systems. It reads and executes commands from a file in the current shell environment. This command is particularly useful for loading environment variables, configurations, or functions intended for use in the current shell session. Unlike running a script in a subshell, using source
ensures that changes made, such as setting environment variables, remain effective in the current shell.
Use case 1: Evaluate contents of a given file
Code:
source path/to/file
Motivation:
This common use case is crucial when you want to update the current shell environment with settings and variables specified in a file without having to start a new shell or left to manually copy these settings. For instance, this could be a Bash configuration file or a script that sets up environment variables required for a particular project. Imagine you’re working on a project that needs specific environment variables set. Instead of manually typing each variable every time you open a terminal, you can keep these in a script file and simply use source
to load them quickly.
Explanation:
source
: The command to execute the scripts or commands within a specified file in the current shell context rather than a separate subshell.path/to/file
: The path argument directs the shell to locate and evaluate the contents of the specified file. This path can be absolute or relative to the current directory.
Example Output:
Upon executing this command, there isn’t any direct output to the screen. However, the result of the command is reflected in the environment of the current shell. For instance, if the sourced file contains export VAR_NAME=value
, then echo $VAR_NAME
after sourcing the file will output value
.
Use case 2: Evaluate contents of a given file (using dot .
)
Code:
. path/to/file
Motivation:
This is another syntactical method of achieving the same result as using source
. The dot (.
) offers a shorthand way to execute the commands within the specified file in the current shell environment. This form is often used in shell scripts due to its brevity and compatibility across different Unix-like systems where source
might not be available or recognized. For example, if you’re writing a setup script that runs across various bash environments, using the dot syntax can ensure broader compatibility.
Explanation:
.
: In shell scripting, the dot command functions identically tosource
. It tells the shell to read and execute commands from the file specified immediately after it within the same shell.path/to/file
: This remains the path of the file containing the commands or scripts that need execution in the current shell. As with thesource
command, the path could be absolute or relative.
Example Output:
As with the source
command, the usage of the dot syntax doesn’t provide explicit output unless there are print statements or errors within the file being sourced. The changes it effects, such as variable assignments or function definitions, can be observed in the shell session after running the command.
Conclusion:
The source
command, along with its alternate form using the dot (.
), provides a seamless way to load environment variables, configurations, or shell functions into the current shell environment. This is particularly useful for streamlining and automating shell setups tailored for specific tasks or projects. Both syntaxes adapt to different Unix-like operating environments, offering flexibility, brevity, and ease of use for shell users.