A Complete Overview of Bash “dot” Command [With 2 Examples]

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.

NOTE: Make sure to add a space between the dot (.) sign & the filename.

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:

Steps to Follow >

➊ 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"

EXPLANATION
Here, #! /bin/bash: ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. Next, 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.

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"

EXPLANATION
The 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

EXPLANATION
  • 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

Load environment varinables using bash dot commandFrom 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:

You can follow the Steps of Example 01, to save & make the scripts executable.

Script (config.sh) >

#! /bin/bash

USERNAME=”leo”
HOST=”linuxsimply.com”

EXPLANATION
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."

EXPLANATION
The script line ‘. 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

Source configuration script file using bash dot commandFrom 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

What does dot command do?
The dot command, also known as the “source” command in Bash, is used to execute the contents of a script within the current shell environment. 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.

How to use dot in Linux?
In Linux, the dot command is used to execute the contents of a script within the current shell. For executing any bash file use the dot command with this syntax, “. /path/to/script.sh”.

What does a single dot mean in Bash?
In Bash scripting, a single dot (‘ . ’) is used as a command called the dot command or source command. It is used to execute the contents of a script within the current shell.

What is two dots in bash?
In Bash, two dots (‘ .. ’) represent the parent directory of the current directory. It is used to reference the parent directory when specifying a file or directory path. It’s also used in the CLI, with the cd command as an argument to navigate up one level in the file system hierarchy.

Related Articles


<< Go Back to Executing Bash Script | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Monira Akter Munny

Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering & Technology in the Electrical & Electronics department. I'm more of an online gaming person who also loves to read blogs & write. As an open-minded person ready to learn & adapt to new territory, I'm always excited to explore the Linux world & share it with you! Read Full Bio

Leave a Comment