11

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…

0

3 Answers 3

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.

2
  • 3
    Moreover, 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
    Sep 15, 2009 at 12:09
  • 1
    See also 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
    Apr 15, 2010 at 6:43
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.

4
  • -1 . <cmd> will look for the program in $PATH and if it is not found THEN it will look in the current directory.
    – dogbane
    Dec 21, 2012 at 13:38
  • @dogbane Right, I corrected this.
    – kmkaplan
    Dec 21, 2012 at 13:41
  • FWIW, not all shells will search cwd for sourced sripts. zsh (at least with the config I have) requires . ./cmd
    – bstpierre
    Dec 21, 2012 at 13:55
  • 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
    Dec 21, 2012 at 14:02
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.

2
  • more precisely, source is a bash-only alias to . (which is standard) Dec 22, 2012 at 12:25
  • You are right. Fixed. Dec 22, 2012 at 13:20

You must log in to answer this question.

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