Why do I need to type `./` before executing a program in the current directory?
When you type the name of a program such as a.out
the system looks for the file in your PATH. On my system, PATH is set to
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Yours is probably similar. To check, enter echo $PATH
in a terminal.
The system looks through these directories in the order given and if it can't find the program produces a command not found
error.
Prepending the command with ./
effectively says "forget about the PATH, I want you to look only in the current directory".
Similarly you can tell the system to look in only another specific location by prepending the command with a relative or absolute path such as:
../
means in the parent directory eg ../hello
look for hello in the parent directory.
./Debug/hello
: "look for hello
in the Debug subdirectory of my current directory."
or /bin/ls
: "look for ls
in the directory /bin
"
By default, the current directory is not in the path because it's considered a security risk. See Why is . not in the path by default? on Superuser for why.
It's possible to add the current directory to your PATH, but for the reasons given in the linked question, I would not recommend it.
The reason for this is simple.
Suppose you have a command with the same name as an application in the current directory. Then running the command in the shell would invoke your app instead of the built-in command. This would be a security concern if nothing else.
By requiring ./
to be used in front, the shell knows that you want to execute the application with the given name and not a built-in command with that name.
./
executes files that are not in your $PATH
, rather it executes the file in the current directory (or another via ./home/stefano/script.sh
). Now, PATH is an environment variable that contains all of the places where bash can look for executable programs, without having the full (absolute) path to them.
This seperation is needed to avoid running the wrong file. I.e. if you have a file called ls
in your home directory, it not being in your PATH will prevent bash from confusing it with the real ls
. The PATH variable also defines the search order:
- When you run a command, or a program tries to make an
exec
syscall (a special method of the Kernel, how programs are started), the system looks for the file by going through each of the directories in your PATH. Once the program has been found, even if it's in multiple directories, the search is interrupted and the first one found is run.
To run a file, you will need to set the executable bit in the permissions:
Since you're already on the command line, you can just type
chmod +x finename
.Or you can set the permissions by right clicking the file and selecting Properties:
You can now copy the file to any of the directories in PATH, to see which ones are in there - and they're set on a per-user basis - type echo $PATH
.
stefano@3000-G530:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
If you create an executable file, cat
, and move it to /usr/local/sbin
, it is run instead of the proper cat
, which resides in /bin
. You can find out where your files are by using type cat
and whereis cat
.