This article examines the difference between the dot and source methods when calling another script in Bash or other shells.
Dot and source are two commonly used methods for executing commands from a file and are often used interchangeably. While dot is the POSIX-standard command, source is a more readable synonym provided by Bash and some other shells.
Despite their different names, Bash treats dot and source as synonyms, and they have the same functionality. Both methods require Bash to be installed and can be used in various shell environments.
This article aims to clarify the distinction between dot and source, provide example usage, and compare the two methods. By understanding the differences and similarities between dot and source, readers will be able to make informed decisions about which method to use when sourcing files in a shell script or on the command line.
Difference in Execution
The difference in execution between using dot and source while calling another script lies in the fact that dot is the POSIX-standard command for executing commands from a file, while source is a more-readable synonym provided by Bash and some other shells.
Bash makes no distinction between dot and source, as they have the same functionality. Both dot and source can be used interchangeably to call another script and have the same effect. Dot and source execute the commands from the file, allowing the script to run.
However, dot is shorter than source, making it less readable and harder for newbies to search for. Despite this difference in syntax, dot and source serve the same purpose and behave uniformly across different shells.
Example Usage
An illustration of the disparity between the two methods can be observed in a scenario where a script is being invoked.
Suppose we have a script called ‘script2.sh’ that contains a function named ‘function2’.
When using the dot method, the script is called with the command ‘. script2.sh’. This causes the script to be executed within the current shell environment, allowing the function ‘function2’ to be directly accessible.
On the other hand, when using the source method, the script is called with the command ‘source script2.sh’. This also executes the script within the current shell environment, making the function ‘function2’ accessible.
In terms of functionality, both methods achieve the same result, but the source method is more commonly used and recommended due to its increased readability and compatibility across different shells.
Comparison of Methods
One way to compare the methods of invoking scripts is to analyze their functionality and compatibility across different shells.
In this case, both the dot and source methods serve the same purpose, which is to execute commands from a file. They have similar functionality and can be used interchangeably in most cases.
However, it is important to note that dot is the POSIX-standard command, while source is a more-readable synonym provided by Bash and some other shells. Despite this distinction, Bash makes no difference between dot and source, and they behave uniformly across different shells.
Both methods can be used to call another script, and they have the same effect as running a script. Therefore, there is no significant difference between using dot or source while calling another script.