How and When to Use the Dot Command in Bash?

  • HOME
  • >
  • BASH
  • >
  • How and When to Use the Dot Command in Bash?
Last Updated: 

First of, the dot command (.) is not to be confused with a dot file or a relative path notation. For example, the ~/.ssh folder is a hidden folder using the dot file notation, you will need to use ls -a to see that folder. And ./ is a relative path to the script named "" in the current folder. Using the Dot command would be as such: .

The dot command (.), aka full stop or period, is a command used to evaluate commands in the current execution context. In Bash, the source command is synonym to the dot command (.) and you can also pass parameters to the command, beware, this deviate from the POSIX specification.

.: . filename [arguments]
    Execute commands from a file in the current shell.
    Read and execute commands from FILENAME in the current shell.  The
    entries in $PATH are used to find the directory containing FILENAME.
    If any ARGUMENTS are supplied, they become the positional parameters
    when FILENAME is executed.
    Exit Status:
    Returns the status of the last command executed in FILENAME; fails if
    FILENAME cannot be read.

👉 Unless you provide an exact path to the filename, Bash will use the $PATH entries to find the file to evaluate.

How to use the Dot Command in Bash?

When you run an executable script as ./, the commands are run in a new subshell, while when run as . the current shell context will be used. Generally, your current context is your terminal window. This mean that the dot command will apply changes to your current shell. Let's look at the simple example below.

#!/usr/bin/env bash
export A="hello world"
echo $A

When run as an executable using ./, the A variable is not exported in your current shell and would just return an empty result.

$ ./ 
hello world
$ echo $A

When run the same script with the dot command using ., your current shell context will be changed.

$ . 
hello world
$ echo $A
hello world

When to use the Dot Command in Bash?

The most obvious use case for using the dot command is when you want to change your current context by setting new variables or changing some existing one. You may also have a script that expect to change your current directory by using cd. It may also be convenient when trying to run a script that doesn't have the execute (x) permission.

Related bash posts that you may like...
The Complete How To Guide of Bash Functions
Learn how to write shell scripts with bash functions. This guide includes examples and best practices on how to define, call, and debug functions in bash.
How To Create Simple Menu with the Shell Select Loop?
The select loop is not a regular shell loop. It can be used in Bash to generate a simple menu from which a user can select numbered options.
What is the Right Way to do Bash Loops?
Looping over a list of numbers or words is a building block in shell scripts. Learn how to write Bash loops, including for loop, while loop, and until loop.
What is the Best Way to Count Files in a Directory?
Learn how to count the number of files in a directory using the Linux command line ls, find, and a native bash shell solution with globs and arrays.