What is the difference between . and ./ in bash? [closed]
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.
./program
runs a file namedprogram
located in your current working directory (./
) (in a new shell for a shell script)..
is the same assource
, 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.
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.