What's the difference between. and. / in bash?

Running this command (where a.out is a valid C executable):

 . a.out 

... results in an error:

bash:.: a.out: cannot execute binary

However, by running the following command:

 ./a.out 

... successfully executes the C executable.

It is clear that there are two types of execution, what is different?

+5
source share
6 answers

The shell uses spaces to separate the command to run and its parameters.

In the first example, the command to run is . with the parameter a.out . Team is a shell shortcut for source , which takes the name of a file containing shell commands as its first parameter, and runs these commands in the current shell. This command fails because a.out is a binary and not a shell script.

In the second example, the command to run is ./a.out , which means launching the a.out file located in the current directory.

+16
source
  • ./program runs a file called program located in your current working directory ( ./ ) ( in a new shell for the shell script).
  • . matches the source that runs the shell script in the current shell . Unlike ./program , it cannot be used to run binary files! As an example, you can use this command to run the .bashrc shell script because you want this script to change your current shell .
+6
source

The command is executed first . (period) with a.out as an argument. The task of the dot command is to parse the text file as commands to execute inside the current shell environment. This gives you an error because a.out not a text file.

The second executes ./a.out , which means "a program named a.out in the current directory.

+4
source

. , only, is the source command. It reads the file and executes it in turn in the current shell, which, as you saw, does not work for binary executable files (unlike scripts).

In the context of the path, for example ./ indicates the current directory. Thus, ./a.out will mean "run the a.out file in the current directory."

+2
source

As everyone says:
. myfile . myfile : executes commands from myfile . (Like the source command in C Shell)
./myfile : executes myfile

To develop,. is the command itself (and myfile is passed as an argument), where ./ is the (relative) path to the file. When executing ./myfile you execute myfile , which is executable, and located in your current directory.

With that said, if you want to execute some kind of executable, for example a.out (which I assume is C or C ++ executable or something like that), you enter ./a.out .
When you have a command in a text file and you want your shell to run them, you enter . myfile . myfile The deepest example of this 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 the command . s which is your current directory (as in the first result of ls -a )

+2
source
 . a.out 

This piece of code will execute the shell script present in a.out using bash in the real terminal.

 ./a.out 

This piece of code does not execute any command. (.) before the file is a hidden file [For ex: .a.out]. Similarly (./) represents folders present in the current working directory. For example:

 cd ./documents 

The above code snippet will change your directory to documents.

+2
source

Source: https://habr.com/ru/post/1275243/


All Articles