The Meaning of dot slash for Running an Executable in Linux

The dot refers to the current directory. The forward slash "/" is the directory path separator.

So, when you type in ./filename at the shell, you prefix the filename with the path to the current directory.

Why do we need to do this when running any executable file?

When you run a command in the shell, it either runs it as a built-in command, or as an executable.

If it isn’t a built-in command, the shell tries looking for the executable in all the directories specified by the PATH environment variable – you can see what this contains by typing in echo $PATH in the terminal.

So if your file is in the current directory, the shell won’t find it – because the current directory is not included in the PATH by default. If you prefix the dot and the slash, the path to the file is passed to the shell, which looks for it exactly at the specified location. In fact, the shell doesn't search the PATH at all when an absolute or relative path (other than just the filename) is specified.

Here’s something to try, but make sure you immediately undo it by exiting the terminal: Add the current directory to the PATH variable in your current shell by typing in

export PATH=$PATH:.

Now, you can run your executable without the dot slash!

WARNING: As noted in the comments below, this change has security implications. The above command will last only for the current terminal session – the changes to the PATH variable will be lost when you exit the terminal. Make sure you do not make this change permanent.

How about using the absolute path?

Since the dot slash helps the shell find the executable by specifying the directory, providing the absolute path without the dot prefix also works.

People might think you always need the dot to be prefixed, even when using absolute paths, but that doesn’t work because you end up prefixing the path to the current directory in addition to the complete path.

Note that you can also execute files using a relative path, in the form of <sub-dir>/<executable>, for example.



Comments

18 years ago when I started using Linux, it was common to have ./ in the users path. They took it out because of security reasons.
It's too easy for someone to put a command in their home directory that matches a commonly used program. Just image if someone make a shell script that contained "rm -rf /" and then named it "ls". Then while admining the machine you, as the root user, simply wanting to get a directory listing, end up wiping out your whole machine simply because it ran the "ls" executable from the current directory first.
You could put the current direct at the end of the path, so that most programs will execute out of the system first, which would give you a little more security, but then all they would have to do it make a file named with a commonly used program but one that you don't have installed, or use a common typo for commands and the same thing happens.

By Brian Cluff

You should mention that adding "." to the PATH variable is extremely dangerous.
That's why it is not done by default, and should *not* be done.

Stupid trick example :
- someone access your keyboard while you drink a coffee. He only has 20 secondes to act.
- he pastes a little "su" (or "sudo") file, that contains a small scripts that calls the "su" command ... but in addition redirects input to a remote logfile.
- next time you run "su" : you won't notice but you won't run the command, instead you'll run the tricky file.
- now your password is compromised.

If "." is not in the PATH,
this kind of trick dos not work.

By TYRCONNEL

Adding the dot (current directory) to the PATH will at best confound and confuse you at some future time, or at worst will be regrettably perilous at some future time.

bjd

By bjd

great explanation!

By laike9m

Add new comment