Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between . and ./ in bash? [closed]

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?

like image 252
Dan O'Boyle Avatar asked Feb 08 '18 17:02

Dan O'Boyle


People also ask

What is difference between and in bash?

The difference is that word splitting and glob expansion are not performed for variables inside [[...]] so quoting the variables is not so crucial.

What does $() mean in bash?

Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).

What is the difference between and == in bash?

In short (in bash): Yes both = and == are the same (inside test constructs).

What is the difference between and >> in the shell environment?

So, what we learned is, the “>” is the output redirection operator used for overwriting files that already exist in the directory. While, the “>>” is an output operator as well, but, it appends the data of an existing file.


2 Answers

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.

like image 175
dbush Avatar answered Oct 01 '22 03:10

dbush


  • ./program runs a file named program 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.
like image 23
Ronan Boiteau Avatar answered Oct 01 '22 03:10

Ronan Boiteau