Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix command line execute with . (dot) vs. without

Tags:

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 
like image 752
Landon Kuhn Avatar asked May 28 '09 19:05

Landon Kuhn


People also ask

What is the use of dot in Unix?

(dot) runs a shell script in the current environment and then returns. Normally, the shell runs a command file in a child shell so that changes to the environment by such commands as cd, set, and trap are local to the command file. The . (dot) command circumvents this feature.

Why do I need dot slash?

This is where the dot slash ./ notation comes in. It means “Look in the current directory.” When you use ./, you tell Ubuntu or Fedora or SUSE or whatever Linux distribution you're using to look in the current directory for the command you wish to run, and completely ignore what's on the application PATH.

Why we use dot in Linux?

(dot dot) means the parent directory of the current directory you're in. For example, if you're in foo/bar/ , . will represent bar/ , .. will represent foo/ .

What does dot slash mean in Unix?

A dot slash is a dot followed immediately by a forward slash ( ./ ). It is used in Linux and Unix to execute a compiled program in the current directory.


1 Answers

. 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 #!/interpreterline), 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.

like image 149
Johannes Schaub - litb Avatar answered Oct 19 '22 17:10

Johannes Schaub - litb