The dot command, also known as the ‘source command‘, is an essential part of Bash that allows users to execute scripts within the current shell. In this writing, I will discuss when & how to use the command with practical examples.
Key Takeaways
- Learning about the Bash dot command.
- Learning how & when to use the dot command in Bash scripting.
Free Downloads
The “dot” Command
The dot command is a special shell built-in command denoted by a single dot (‘ . ’) that executes the content of a file and is passed as an argument in the current shell session. When you use this command to execute a script, it reads & executes commands from the specified script file as they were typed directly into the current shell session.
Command Syntax >
. filename
Here, the filename can be with the absolute or relative path location of the script file.
When to use the Dot Command in Bash?
In bash, use the “dot” command when you want to change your current context by setting new variables or changing some existing ones. Moreover, use the command,
- To run a shell script that needs to be executed in the current shell context, for example, a script that uses the cd command to change the current directory, you can use the dot command in Bash.
- To run a shell script that does not have execute permissions.
- To source a configuration file, functions from another script or just to modify environment variables.
Difference Between Dot Command & Other Uses of Dot Symbol
DO NOT confuse the dot command with the dot file or relative path notation. A dot file is a file whose name starts with a dot (.), also known as a “hidden” file. And these hidden files or directories’ names starting with a dot are not shown in listings by default.
Another use of the dot symbol is in directory & file paths, a single dot (.) is used to represent the current directory, and a double dot (..) represents the parent directory.
2 Practical Examples of Bash Dot Command
The dot command is basically a shorter approach for the source command. In other words, a symbol (.) that represents the source command. So, you also check ‘What are the Usages of Bash Source’ for learning basic concepts of bash sourcing. Anyway, here I will show two practical examples using the dot command.
Example 1: Loading Environment Variables Using Bash Dot Command
Loading environment variables is particularly useful when you have multiple scripts that require the same set of variables. Instead of duplicating the variable definitions in each script, you can store them in a file & source that file using the dot command in each script. Go through the following steps to see the process:
➊ At first, launch an Ubuntu Terminal.
➋ Now, write the following command to open a file in the nano text editor:
nano env_load.sh
➌ Afterward, write the following script in the nano editor:
Script (env_var.sh) >
#!/bin/bash
export DATABASE_HOST="localhost"
export DATABASE_USER="myuser"
export DATABASE_PASSWORD="mypassword"
➍ Then, press CTRL+S to save the file & press CTRL+X to exit the nano editor.
➎ After that, write the following script file (load_env_var) in the same way as the previous script & save it.
Script (load_env_var.sh) >
!/bin/bash
# Load the environment variables
. env_var.sh
# Access and use the environment variables
echo "USER: $DATABASE_USER"
echo "HOST: $DATABASE_HOST"
echo "PASSWORD: $DATABASE_PASSWORD"
➏ Then, use the following command to make the script executable:
chmod u+x load_env_var
- chmod: Changes the permission of files and directories.
- u+x: Argument with chmod command to add the executable permission for the user.
- load_env_var: File which you want to make executable.
➐ Finally, write the following command to run the ‘load_env_var.sh’ script to check if the variables in the ‘env_var.sh’ script have been loaded successfully or not:
./load_env_var.sh
From the output image you can see the environment variables in the ‘env_var.sh’ script have been sourced successfully.
Example 2: Sourcing Configuration File Using Bash Dot Command
In this example, I will show how you can source the configuration variables from ‘config.sh’ & use them in my main script named ‘run_config.sh’ using the dot command. This way you can easily include and use configuration settings from a separate file within your main script.
For that, let’s check out the contents of the two scripts:
Script (config.sh) >
#! /bin/bash
USERNAME=”leo”
HOST=”linuxsimply.com”
Script (run_config.sh) >
!/bin/bash
# Source the configuration file
. config.sh
# Access and use the configuration variables
echo "Connecting to $HOST with username $USERNAME."
Now, write the following command to run the ‘run_config.sh’ script to check if the variables in the ‘config.sh’ script have been loaded successfully or not:
./run_config.sh
From the output image you can see the variables in the ‘config.sh’ script have been sourced successfully.
Conclusion
In conclusion, the dot command is a powerful tool to run shell scripts from within the current shell. Moreover, it can also be used in a variety of situations due to its versatile usage areas. Hope this article helps you to understand the importance & usage of the command.
People Also Ask
Related Articles
- How to make a File Executable in Bash [2 Methods]
- What are the Usages of Bash Source [3 Practical Examples]
- How to Run a Bash Script [2 Methods with Cases]
- How to Run Bash Commands in Parallel [4 Ways with Examples]
<< Go Back to Executing Bash Script | Bash Scripting Basics | Bash Scripting Tutorial