31

When using bash shell, I sometimes keep environment variables in a text file which I copy/paste the content of, eg exports.txt:

export FOO=bar
export FIZZ=buzz

Someone showed me instead of copy/paste, I could type in the terminal

. exports.txt

which would have the same effect as copy/paste.

What is the mechanism by which this 'dot space filename' command works? It's hard to think of search terms for it.

I want to understand what is happening and the more general details of what this one-liner is doing.

43

The . ("dot") command is a synonym/shortcut for the shell's built-in source command.

It causes the named shell script to be read in and executed within the current shell context (rather than a subshell). This allows the sourced script to modify the environment of the calling shell, such as setting variables and defining shell functions and aliases.

| improve this answer | |
  • 49
    Actually, source is a non-standard and non-portable synonym/shortcut for the POSIX-defined "dot" (.) command, not the other way around. – terdon Oct 19 '16 at 10:28
  • 8
    bash provides a non-standard source and a non-standard . in non-POSIX mode, both of which search the current directory even if it isn't part of $PATH. In POSIX mode, it provides a standard . which doesn't search the current directory, and no source. In neither mode is source synonymous with POSIX's . command. – hvd Oct 19 '16 at 20:06
28

While the two existing answers are already excellent, I feel the example where the effect is the most "noticeable" so to say, is missing.

Say I have a file script.sh with the following contents:

cd dir

If I would run this script normally (sh script.sh), I'd see this:

olle@OMK2-SERVER:~$ sh script.sh
olle@OMK2-SERVER:~$

But if I where to source the script (. script.sh), I'd end up with this:

olle@OMK2-SERVER:~$ . script.sh
olle@OMK2-SERVER:~/dir$

Notice how in the second case the working directory of our main shell has changed!

This is because (as pointed out in the other answers) the first example runs in its own subshell (the sh process we start with the sh-command, this could have been basically any shell, bash, dash, you name it), it changes directory there, does nothing and closes. While the second example runs in our main shell, and thus changes directory there!

| improve this answer | |
5

Here is an example.

Script file: mytest.sh

cat mytest.sh

#!/bin/bash

myvar=1
mystring="Hello World"

if you try to print any of the above variables you will get nothing

echo $myvar

but if you do

. mytest.sh

or

source mytest.sh

and then

echo $myvar

it will print 1

Just a visual answer of what Spiff wrote

| improve this answer | |
  • So interpreting your example, using export is only if those variables are to be used in subshells. I could omit export in the file if the variables are only to be used in the current shell. Is that right? – iancoleman Oct 19 '16 at 2:36
  • 1
    Quite right and I could say yes. However the use of the export command is more useful or more understandable if you think about the shell environment variables. For example $HOME or $DISPLAY. Yes you could use the export command to export a variable on your shell session or any subshells but it will gone as soon as you terminate that session. – raism Oct 19 '16 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.