Calling a script with ./bla.sh vs. . bla.sh

11

1

Can anybody explain to me what the shell does in the two examples A) and B) below? It obviously behaves differently, but I can't find out why the output is different.

Example:
Let's have a script in our current directory named bla.sh with only one command:
echo ${0##/*} hello

A)
Started as: ./bla.sh
gives: ./bla.sh hello

B)
Started as: . bla.sh
gives: -bash hello

Since I use this in a script, the second output (because of the "-" in front of the -bash) kills the command. Of course, a simple -- before the ${...} helped, but I would love to understand what causes the output in the first place.
I love bash. And vi[m]. But I digress…

Wolf

Posted 2009-09-13T11:05:24.320

Reputation: 2 425

Answers

22

./bla.sh

Here, the command is ./bla.sh. This makes the shell look for an executable named bla.sh in the current directory, then ask the kernel to run it as a normal program, in a separate process from the shell. (It doesn't matter if bla.sh is a bash script, a perl or python one, or a compiled binary.)


. bla.sh

Here, the command is . (aka source), a built-in command of your shell. It makes the shell look for a file named bla.sh in the system path ($PATH) and interpret the contents as if they were typed by you; all this is done in the same process as the shell itself (and therefore can affect the shell's internal state).

This of course only works when bla.sh contains commands for the bash shell (if that's the one you are currently using), it won't work for perl scripts or anything else.

(This is explained in help . and help source too.)


As . and ./ are completely different things (a command vs part of a path), they can be combined, of course – using . ./bla.sh would "source" a file bla.sh in the current directory.


Usually it is best to use the ./bla.sh method. Only ~/.bashrc, ~/.profile and such files are usually sourced, because they are supposed to modify the current environment.

user1686

Posted 2009-09-13T11:05:24.320

Reputation: 283 655

1

See also http://mywiki.wooledge.org/BashFAQ/060 for some examples. Note that source is a bash alias for ., not the contrary and source won't work in other shells.

– mrucci – 2010-04-15T06:43:10.353

3Moreover, if you change bash environment in bla.sh, these changes are taken into account after . bla.sh but not after ./bla.sh. this is because . bla.sh runs in the context of the current bash whereas ./bla.sh runs as a subprocess. – mouviciel – 2009-09-15T12:09:51.943

7

./<cmd> will execute the <cmd> program that resides in the current directory in a new (forked) process. It has to be executable. And also readable it starts with #!.

. <cmd> will make your current shell execute the shell script <cmd> that resides in your $PATH or the current directory in the current shell process. It has to be readable. It is an alias for the shell command source.

kmkaplan

Posted 2009-09-13T11:05:24.320

Reputation: 334

-1 . <cmd> will look for the program in $PATH and if it is not found THEN it will look in the current directory. – dogbane – 2012-12-21T13:38:40.417

@dogbane Right, I corrected this. – kmkaplan – 2012-12-21T13:41:20.167

FWIW, not all shells will search cwd for sourced sripts. zsh (at least with the config I have) requires . ./cmd – bstpierre – 2012-12-21T13:55:50.970

1@bstpierre This seem to be a moving ground. The POSIX reference I have says that “the shell shall use the search path specified by PATH” and “Some older implementations searched the current directory for the file, even if the value of PATH disallowed it.” – kmkaplan – 2012-12-21T14:02:31.053

1

./cmd uses explicit path (./ - current dir) to executable. And it is not necessary that it starts with #!.

. cmd - (aka source) - bash builtin command. One visible difference of executing through source is that it can set/modify environment variable of current shell.

Leonid Volnitsky

Posted 2009-09-13T11:05:24.320

Reputation: 8 235

more precisely, source is a bash-only alias to . (which is standard) – user1686 – 2012-12-22T12:25:43.157

You are right. Fixed. – Leonid Volnitsky – 2012-12-22T13:20:39.950