I have made my Python script executable with chmod +x
and now I can run it from the terminal with ./ prefix (./script_name).
What exactly does this prefix mean? Why is it needed to run an executable file?
I have made my Python script executable with chmod +x
and now I can run it from the terminal with ./ prefix (./script_name).
What exactly does this prefix mean? Why is it needed to run an executable file?
Like any other program, a shell is also a program which is waiting for input. Now when you type in command1 arg1 arg2 ...
, the first thing a shell does is to try to identify command1
from among the following:
typeset -f
in Bash shell)type
)alias
in Bash shell)Now the question concerns the last point a file that can be executed. A Unix kernel will need absolute path of an executable file in exec()
system call (see man exec
).
To obtain the absolute path of a file, the shell first looks up the command in directories specified in $PATH variable.
So if you specify the relative path such as ../abc/command1
or ./command1
then Bash will be able to find that file and pass it to exec()
system call.
If all the above four steps fail to locate the command1
input to the shell, you will get:
$ command1
command1: command not found
However, if the file's absolute path is resolved, but it is not executable, you get:
$ command1
bash: ./command1: Permission denied
References:
It means the current directory and the script is in the current directory.
The OS searches your $PATH
when using a bare executable name (foo
vs ./foo
). In your case, the file may not reside on the path, so you need to tell the OS exactly where it is. You do that by specifying the path to the executable. That's what the ./
i saying: look for the executable in my current working directory. It's called a "relative path", and they're handy for when the thing you want to reference is close to your current working directory.