Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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

Started as: ./
gives: ./ hello

Started as: .
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…

share|improve this question
up vote 22 down vote accepted

Here, the command is ./ This makes the shell look for an executable named 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 is a bash script, a perl or python one, or a compiled binary.)


Here, the command is . (aka source), a built-in command of your shell. It makes the shell look for a file named 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 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 . ./ would "source" a file in the current directory.

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

share|improve this answer
GREAT! Thanks a lot! – Wolf Sep 13 '09 at 12:36
Moreover, if you change bash environment in, these changes are taken into account after . but not after ./ this is because . runs in the context of the current bash whereas ./ runs as a subprocess. – mouviciel Sep 15 '09 at 12:09
See also 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 '10 at 6:43

./<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.

share|improve this answer
-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 '12 at 13:38
@dogbane Right, I corrected this. – kmkaplan Dec 21 '12 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 '12 at 13:55
@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 '12 at 14:02

./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.

share|improve this answer
more precisely, source is a bash-only alias to . (which is standard) – grawity Dec 22 '12 at 12:25
You are right. Fixed. – Leonid Volnitsky Dec 22 '12 at 13:20

You must log in to answer this question.

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