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

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.

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

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:

  1. At first, launch an Ubuntu Terminal.
  2. Now, write the following command to open a file in the nano text editor:
    nano env_load.sh
  3. 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.

  4. Then, press CTRL+S to save the file & press CTRL+X to exit the nano editor.
  5. 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"

    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.
  6. Then, use the following command to make the script executable:

    chmod u+x load_env_var
  7. 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.

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

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.

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

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