I want to ask a question about bash and source.

I acknowledge they are both used with .sh extension files such as

bash setup.sh
source setup.sh

However, I wanted to know a difference between those two if one could explain.

There is a post Using source vs bash commands, which attempted to explain, but I could not clarify if the responder tried to say "source" ignores past definition and sets environment variables in current shell or bash ignores past definition and sets environment variables in current shell.

If one could provide a better description of difference between source and bash, that would be great as well.

Thank you!

  • 1
    Unix/linux file extensions are not required at all. They generally just act as hints to your editor or default appliation launcher. Nov 26, 2018 at 17:49

2 Answers 2


The link you provided does not mention "past definition", probably because this does not matter. source operates in the current shell and can affect variables in the current shell. bash invokes a new shell and that shell cannot affect the environment of its parent. Also, the .sh extension is completely optional and is specifically not used in shell initialization files.

  • 1
    Technically "specifically not used in shell initialization files" is not true: bash login shells source /etc/profile and that file does for i in /etc/profile.d/*.sh Nov 27, 2018 at 21:37

What happens when you run bash foo.sh ? A fresh bash process starts and reads and executes the commands that are in foo.sh. When it's done executing the commands, this new process exits and you get your prompt back, echoed by your current shell.

What happens when you run source foo.sh (or . foo.sh) ? You basically tell your current shell "instead of reading commands from the keyboard, read commands from this file, when you're done, come back to the keyboard"

When does it matter ? When there are commands that change the state of the shell; for example cd, exit, VAR=value, alias and a few other.

Suppose foo.sh contains this :

mkdir /tmp/bar

It does not matter if you run it with source or bash. Now, suppose the content of the script changes variables, then it matters. When you run bash foo.sh, a new bash is created, it runs the commands and exits; no change in your shell. If, on the other side, you source foo.sh, your current shell variables are changed.

Let's take an example. Suppose there is this in file foo.sh:

echo "Currently executing commands from foo.sh"
echo "changing variable foo."
echo "In shell with PID $$, the value of foo is now >>$foo<<"
echo "End of foo.sh"; echo

What happens if you run bash foo.sh and then echo $foo ? What happens if you run source foo.sh and then echo $foo ?

Try multiple times. You'll notice that if you run bash foo.sh, the foo variable is not set in your shell. if you run source foo.sh, the foo variable is set (and changed) each time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .