What's the meaning of a dot before a command in shell?

A dot in that context means to "source" the contents of that file into the current shell. With source itself being a shell builtin command. And source and the dot operator being synonyms.

Example

Say I had the following contents in a sample.sh file.

$ cat sample.sh 
echo "hi"
echo "bye?"

Now when I source it:

$ . sample.sh 
hi
bye?
$

Files such as this are often used to incorporate setup commands such as adding things to ones environment variables.

Examples

Say I had these commands in another file, addvars.sh.

$ cat addvars.sh 
export VAR1="some var1 string"
export VAR2="some var2 string"

Notice that I don't have any variables in my current shell's environment.

$ env | grep VAR
$

Now when I source this file:

$ . addvars.sh 
$

OK, doesn't seem like it did anything, but when we check the env variables again:

$ env | grep VAR
VAR1=some var1 string
VAR2=some var2 string

To add to slm's answer:

There are two ways to run a shell script. One is to run the script in a separate process, which means anything about the shell's environment (memory state) will revert to the state of the "parent" shell before running the "child" shell process.

For example, the current working directory (the location in the filesystem one is in) is determined on a per-process basis. So, let's have a script that looks like this:

#!/bin/bash
cd ~
cd ..
pwd

So, let's call this script, oh, foo. And let's run this script as follows: ./foo

We will see the following:

/home

(Standard disclaimer that there are a large number of Linux and other UNIX clone distributions out there, some of which do not put user's directories in /home. Or, as we used to say "Your mileage may vary")

Now, after running this script, let's type in this command

pwd

To see which directory we are in. We will see something like this:

/home/username

The reason being, again, the shell script we ran had its own environment (including its own directory where commands were being run), and that environment went away once the script finished running.

Now, let's run the foo script like this

. ./foo

Or, equivalently:

source ./foo

If we do a pwd afterwards, we will see this:

/home

The reason being: Sourcing a script doesn't call a separate process. It is like typing all of the commands in the parent process by hand; its environment is preserved after the script ends.


Let me come up with a simpler example. Let's have a script that looks like this:

#!/bin/bash
exit

Let's name it foo. Let's make sure we can run it: chmod 755 foo. Then, let's run it like this:

./foo

Nothing happens. However, on the other hand, if we do this:

. ./foo

Or this:

source ./foo

We log out.


The period (dot) is short hand for the bash built in source. It will read and execute commands from a file in the current environment and return the exit status of the last command executed. The files can be in the current directory or anywhere in the PATH. It does not need to be executable.

Tags:

Shell