How does "." work exactly when the path directory is included and not included to execute a script file?

Hello Friends

Suppose the following structure

/
 snap
  bin
   notepadpp  <--- file

If pwd returns /snap/bin/ to execute the script happens the following:

  • a: ./notepadpp the execution works
  • b: notepadpp the execution fails

If pwd returns /home/manuel to execute the script happens the following:

  • c: ./snap/bin/notepadpp fails the execution
  • d: /snap/bin/notepadpp works the execution

Why when the path directory is included is mandatory not use the dot?

I know . is about relative path to work with directories’ location, such as: to change the current location through the cd command, and for others jobs such as cp and mv - but about scripts - to be honest: I assumed a and c are the same, without and with the path directory.

Why this behavior about the . when the path directory is included and not included to execute a script file?

The . always means the current directory, regardless of what is in the path
so
./snap does not exist… snap is in / not in /home

2 Likes

I think, this is one of the questions, new users are often afraid to ask, but if you don’t know the answer, it can drive you mad.

So: Thanks for asking:

In the following, d $ c always means: Your current working directory is d and you are issuing the command c.

. always stands for the current working directory. It’s an abbreviation, like ~ stands for your home directory, like /home/manuel

If you issue a command, your shell (i.e. your computer) needs to know where to find it.

One way of doing so, is by telling it, the app’s exact location, so, no matter where you are:

some_directory $ /snap/bin/notepadqq will always work.

In order to make things easier, you can abbreviate this, by using the dot .:

/snap $ ./bin/notepadqq will work, so will /snap/bin $ ./notepadqq

What will not work, is: /home/manuel $ ./snap/bin/notepadqq Why?

Because the dot . is an abbreviation for the current working directory, so the command, you issued was effectively: /home/manuel/snap/bin/notepadqq, but the app is not in this location. It has to fail.

Now, one more recommendation.

If you don’t tell your computer, where a program is located, it will always look in a list of directories, if it might be there. This list is called The Path.

Some typical directories in The Path are:

/usr/bin or /usr/local/bin.

Usually /snap/bin should be in there, too.

Check it out by typing echo $PATH and hitting Enter
In my case, I get

/home/mina/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

If your PATH already contains /snap/bin, you don’t need to tell your computer, where your app is located, just typing notepadqq, regardless of where you are, will always launch your app.

If your Path doesn’t contain /snap/bin, we’re going to change that now.

Please open your /home/manuel/.bashrc file with the editor of your choice, go to the end of the file and add a line: export PATH=“/snap/bin/:$PATH”

Save the file.

Now, after you restart your computer, you should be able to just type notepadqqand launch your app.
If you don’t want to wait, type source ~/.bashrc, hit Enter and it will also work.

This last command tells the shell to read its configuration file (which it usually only does at login).

I hope, I could help you a bit.

1 Like