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?

0

3 Answers 3

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:

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer and the references, I will read it.
The first link is (effectively) broken: "linuxforums.org is parked free, courtesy of GoDaddy.com."
2

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

1 Comment

Please stop up-voting me! It is a trivial question and the answer is trivial
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.

1 Comment

SO if it was added in the $PATH,the ./ would not be needed?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.