All About the Dot Slash Issue

Dot Slash Command LineIn an upcoming series I’m going to start exploring the basics of shell scripting (writing your own little programs that run from the command line and make things happen).

In the meantime — with not a little prompting from my Dad — I’ve decided to explore the dot slash issue.

I call it an issue, but really it’s just two keystrokes resulting in two characters that get placed before shell scripts to execute said shell scripts.

The dot slash ./ has to be used in order to execute a shell script, even if — or rather, especially if — the script is in your current directory.

Ok. Fine. Sure. Whatever. Can do.

But why?!

That is the million dollar question that I set out to answer.

Breaking Down the ./

It looks simple enough at first glance:

The dot . indicates a location — aka the current directory — as we recently covered.

The slash / indicates a separation between file system objects (and/or is a reference to the root directory).

Put together, the dot slash just seems to say “the current directory”.

It is, in fact, an abbreviation of the absolute path to the current directory.

Simple, right?

If you’ve used the command line, then you know that you can usually reference files in the current directory by just using their name without the dot slash.

Why are shell scripts an exception?

The answer to that is to be found deep in the tangled web of how the shell works.

Commands & Executables

A shell is a text-only interface that executes commands given to the command line.

You already know that commands are tools that tell the operating system to do things.

What you may not already know is that they are two types of commands: built-in and executable.

Built-in commands are a part of the shell, and can always be located by the shell.

Executable commands — compiled programs and shell scripts, both of which have to be converted (compiled/interpreted) for their instructions to be carried out — are not a part of the shell, and so cannot be located by the shell without help.

A shell assumes that anything typed in is a command. To that end, it checks for built-in commands first, and then for an absolute path to an executable file. If neither are found, the shell will check the user’s PATH environmental variable (try echo $PATH to see the contents of yours) and search in each of those directories for a file with the specified name.

If each of these searches turn up empty, an error message will be returned.

It all boils down to the lack of an absolute path, which, as previously mentioned, is resolved by the dot slash abbreviation indicating an absolute path to the current directory.

Security

There are several reasons that the dot slash necessity — or rather, the need for an absolute path name — exists, most of which boil down to security.

One example is as a safeguard against accidentally — or maliciously — creating a shell script with the same name as an existing built-in command, resulting in the fact that the shell script would/could be executed instead of the built-in command, when in the same directory as the shell script. Now imagine that the code in that shell script could delete the contents of your hard drive(s?!), and was run accidently because of its confusing name and no absolute path safeguards. Not good.

There are ways to both downgrade and eliminate the security measures that the dot slash provides, but I’m starting to take comfort in it, so you’re on your own with those solutions.

How hard is it to type in two extra characters, anyway?

Executing Shell Scripts

Shell scripts can be executed via several means, including the dot slash, but not limited to it.

./gwenscript uses the dot slash to execute a file in the current directory.

/home/gwen/gwenscript uses the full file path to execute a script whether or not it is in the current directory.

sh gwenscript and bash gwenscript specify the interpreter that should be used to execute the script, eliminating the need for the dot slash.

Conclusion

If my long and rambling explanation has not been helpful, I would suggest that you look up “what is dot slash” for further reading, in hopes that someone is able to provide a more coherent coverage than I was able to.

Otherwise, share your expertise in the comments!

Leave a Reply

You have to agree to the comment policy.

Scroll to top