3

For instance, I have installed java and can just run java ... but if I create a shell script for instance, I have to run ./script.sh.

Why is this? What does the ./ signify?

1
  • 7
    This answer on StackOverflow covers it very well.
    – Mono
    Apr 23, 2013 at 15:52

4 Answers 4

9

When an executable is not in the PATH , you need to specify the path explicitly. ./ is a "relative" path specifier: "starting from here (.) go "no further" (/). If you add the current directory to your PATH environment variable and issue rehash command, you will no longer need it.

3

When you run a command, the shell searches the PATH variable (or some hash table, depending on the shell#) where to find the executable. But usually the current working directory (.) isn't included. So, you need to tell the shell where to find your script by prepending ./ as explained by Floris.

The purpose of that default setting is, that you are saved from accidentally executing a script (in the current dir) which is named e.g. rm instead of the expected command in /bin. This is especially crucial for root, because the local script can behave completely different as you'll expect it!


# I wrote a little bit more about hash tables in this answer.

1

Current directory is not normaly in PATH, so commands will not lookup from current directory by default

1
  • 2
    Just as extra information: There is a reason why it is not in your path by default, and why you should always put it last if you add it. (Imaging me having a program called ls or dir in my homedir. If you visit my homedir ans try to list the contents then you really want the system file, not the one in my homedir).
    – Hennes
    Apr 23, 2013 at 15:58
1

java is in your $PATH variable so that is why it is found and executed. ./ tells the command line to use the file in your current directory which is usually not on the PATH.

Not the answer you're looking for? Browse other questions tagged .