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
(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.
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.
(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/ .
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.
. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With