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?
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.
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.
Current directory is not normaly in PATH, so commands will not lookup from current directory by default
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).
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.