0

While I was executing a C program in terminal I wrote:

gcc -o demo demo.c
./demo

Actually my doubt is why we write ./ before demo and what does it mean?

Radu Rădeanu
  • 169,590

4 Answers4

2

When we execute any command in the terminal it is going to look for it in the PATH environment variable. By using ./ (relative path) we are telling the shell to look for that command in our current directory.

Check echo $PATH

There are two way to execute any commands

  1. Putting its path in the PATH environment variable
  2. Using the full path or a relative path to that programme (Ex. /opt/xyz/bin/someprogramme )
Luís de Sousa
  • 13,227
  • 26
  • 81
  • 128
Nischay
  • 3,693
1

When bash interprets the command line, it looks for commands in locations described in the variable $PATH. To see it type:

echo $PATH

Normally, he current directory is not in that list. The reason for not having the current directory on that list is security.

The ./ says: look in the current directory for my command rather than looking at all the directories specified in $PATH.

More about:

Radu Rădeanu
  • 169,590
0

You put "./" before every program you wish to execute.

Otherwise the terminal doesn't know what to do with "demo" file

Naveen
  • 9,365
  • 11
  • 43
  • 70
0

The "./" before a file name stand for "execute this file in th current directory" if the file that you want to execute is not in the same directory you are in you will need to specify the path o the file like this:

Xxxxx@xxxx /Documents/filetoexecute

Evgeny Danilenko
  • 156
  • 1
  • 2
  • 9