tags:

views:

671

answers:

5

At a unix command line, what's the difference between executing a program by simply typing it's name, vs. executing a program by typing a . (dot) followed by the program name? e.g.:

runme

vs.

. runme
+3  A: 

Using "source" or "." causes the commands to run in the current process. Running the script as an executable gives it its own process.

This matters most if you are trying to set environment variable in current shell (which you can't do in a separate process) or want to abort the script without aborting your shell (which you can only do in a separate process).

dmckee
+1  A: 

The first executes the command. The second is shorthand for including a shell script inside another.

Mr. Shiny and New
+2  A: 

This syntax is used to "load" and parse a script. It's most useful when you have a script that has common functionality to a bunch of other scripts, and you can just "dot include" it. See http://tldp.org/LDP/abs/html/internal.html for details (scroll down to the "dot" command).

Mike
+10  A: 

. name sources the file called name into the current shell. So if a file contains this

A=hello

Then if you sources that, afterwards you can refer to a variable called A which will contain hello. But if you execute the file (given proper execution rights and #!/interpreter line), then such things won't work, since the variable and other things that script sets will only affects its subshell it is run in.

Sourcing a binary file will not make any sense: Shell wouldn't know how to interpret the binary stuff (remember it inserts the things appearing in that file into the current shell - much like the good old #include <file> mechanism in C). Example:

head -c 10 /dev/urandom > foo.sh; . foo.sh # don't do this at home!
bash: �ǻD$�/�: file or directory not found

Executing a binary file, however, does make a lot of sense, of course. So normally you want to just name the file you want to execute, and in special cases, like the A=hello case above, you want to source a file.

Johannes Schaub - litb
+1  A: 

Running "runme" will create a new process which will go on its merry little way and not affect your shell.

Running ". runme" will allow the script "runme" to change your environment variables, change directories, and all sorts of other things that you might want it to do for you. It can only do this because it's being interpreted by the shell process that's already running for you. As a consequence, if you're running bash as your login shell, you can only use the "." notation with a bash script, not (for example) a binary on C shell script.

Dietrich Epp