Shell – How does the shell interpret ././command-name

directoryfilenamesshellslash

I have a binary file named hello2 in my current working directory.

To execute it I need to press ./hello2 and it shows the output.

But when I use the following command ././hello2 it still works.

Can you please explain how the shell is interpreting this command?

Best Answer

When you run the command

$ ./hello2

the shell looks up the file hello2 in the directory ., i.e. in the current directory. It then runs the script or binary according to some rules (which are uninteresting in this context).

The command

$ ././hello2

also causes the shell to execute the file.

This is because . and ./. is the same directory.

Every directory has a . directory entry. This entry corresponds to the directory itself. So saying ./ is the same as saying ././ and ././././ etc.

The only difference is that the system might have to do a few extra directory lookups (unless the shell is smart and spots the obvious simplification).


Every directory also has a .. entry which points to its parent directory.

This means that if the current directory is called alamin, then the following would also execute the file:

$ ../alamin/hello2

as would

$ ../alamin/./hello2

and

$ .././alamin/./hello2

The root directory, /, is a special case. Its .. directory entry is the same as its . directory entry. This means that you can't go "above" it with /...

See also: The number of links for a folder doesn't reflect the real status?

Related Question