问题
Running this command (where a.out
is a valid C executable):
. a.out
...results in this error:
bash: .: a.out: cannot execute binary file
However, running the following command:
./a.out
...successfully executes the C binary executable.
Clearly, there are 2 types of executions happening here, what's different?
回答1:
The shell uses spaces to separate the command to run and its parameters.
In the first example, the command to run is .
with a parameter of a.out
. The .
command is a shell shortcut for source
, which takes the name of a file containing shell commands as its first parameter and runs those commands in the current shell. This command fails because a.out
is a binary file, not a shell script.
In the second example, the command to run is ./a.out
, which means run the file a.out
residing in the current directory.
回答2:
./program
runs a file namedprogram
located in your current working directory (./
) (in a new shell for a shell script)..
is the same as source, which runs a shell script in your current shell. Unlike./program
, it can't be used to run binaries! As an example, you could use this command to run your.bashrc
shell script, because you want this script to modify your current shell.
回答3:
The first runs the .
(dot) command with a.out
as its argument. The dot command's job is to parse a text file as commands to execute inside the current shell environment. It gives you an error because a.out
is not a text file.
The second executes ./a.out
which means "a program named a.out
in the current directory.
回答4:
.
, alone, is the source
command. It reads a file and executes it line-by-line, in the current shell - which, as you've seen, does not work for binary executables (as opposed to scripts).
In the context of a path, e.g., ./
, .
stands for the current directory. So ./a.out
would mean "run the file a.out in the current directory".
回答5:
. a.out
This piece of code will execute the shell script present in the a.out using bash in the present terminal.
./a.out
This piece of code does not execute any command. (.) dot operator before a file represents a hidden file [For ex: .a.out]. Similarly (./) represents folders present in the current working directory. For ex :
cd ./documents
The above piece of code will change your directory to documents.
回答6:
As everyone is saying:. myfile
: executes commands from myfile
. (Like the source
command in
C Shell)./myfile
: executes myfile
To elaborate, .
is a command by itself (and myfile
is passed as an argument to that), where ./
is a (relative) path to a file. When executing ./myfile
you are executing myfile
which is an executable and is located in your current directory.
With that being said, when you want to execute some executable like a.out
(which I assume is a C or C++ executable or something similar) you type ./a.out
.
When you have a bunch of commands in a "text" file and you want your shell to run those, you type . myfile
. The most profound example of that is probably when you change the contents of .bashrc
or .profile
files and you want to "apply" your changes to the system.
Finally, do not confuse .
command with .
which is your current directory (as in the first result of ls -a
)
来源:https://stackoverflow.com/questions/48691687/what-is-the-difference-between-and-in-bash