FUNDAMENTALS A Complete Guide for Beginners
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. In this writing, I will discuss when & how to use the command with practical examples.
Syntax of “dot” Command
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. 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. The syntax of “dot” command is:
. filename
Here, the filename can be with the absolute or relative path location of the script file.
2 Uses of “dot” Command in Bash
The dot command is a shorter approach for the source command. In other words, a symbol (.) that represents the source command. Here’s how you can use it:
1. Load Environment Variables
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:
#!/bin/bash export DATABASE_HOST="localhost" export DATABASE_USER="myuser" export DATABASE_PASSWORD="mypassword"
The script exports three environment variables named
DATABASE_HOST
,DATABASE_USER
, &DATABASE_PASSWORD
using the export command. - 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.
!/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"
EXPLANATIONThe dot command sources the env_var.sh script using. env_var.sh
command and then echoes the value inside those variables using the ‘echo command.’ -
Then, use the following command to make the script executable:
chmod u+x load_env_var
-
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.
2. Source Configuration File
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:
#! /bin/bash
USERNAME=”leo”
HOST=”linuxsimply.com”
The script contains variable definitions for USERNAME
and HOST
.
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."
. config.sh
uses the dot command to source the ‘config.sh’ file. After sourcing, they can be accessed directly. Which we will see as output after executing the script through echo “Connecting to $HOST with username $USERNAME.” this line.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.
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.
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