1

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3214667
  • 57
  • 1
  • 1
  • 2

3 Answers3

3

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:

  1. a function (try typeset -f in Bash shell)
  2. an in-built command (such as type)
  3. a shell alias (try alias in Bash shell)
  4. a file that can be executed

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:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tuxdna
  • 8,257
  • 4
  • 43
  • 61
2

It means the current directory and the script is in the current directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

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.

John Szakmeister
  • 44,691
  • 9
  • 89
  • 79