Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I insert, for example, unix.stackexchange.com followed by Enter in terminal, I get the following error:

unix.stackexchange.com: command not found

This is ok and as I expected. But when I insert http://unix.stackexchange.com, I get another error message:

bash: http://unix.stackexchange.com: No such file or directory

I do not ask about why I get errors. I want to know why these are different and, eventually, which process/function handles them?

share|improve this question
    
They are different because the latter contains forward slashes (/), which causes you shell to interpret it as a file path. Keep in mind that / is the directory separator in Unix-like OS:s, and cannot be present in a filename. –  Thomas Nyman Oct 25 '13 at 6:41
    
@ThomasNyman Ok, but do you know which processes/functions handles these errors? –  Radu Rădeanu Oct 25 '13 at 7:25
1  
@RaduRadeanu The errors originate from your shell, bash in this case. See my answer below for more details on the code paths which lead to the respective errors. –  Thomas Nyman Oct 25 '13 at 8:07

2 Answers 2

up vote 2 down vote accepted

As also ewhac pointed out, the error messages differ because the latter command line contains forward slashes (/), which causes your shell to interpret it as a file path.

Both errors originate from your shell, which in this case is bash (which is evident from the second error message).

More specifically, the first error originates from the execute_disk_command() function defined in execute_command.c in the bash-4.2 source code. The execute_disk_command() function calls search_for_command() defined in findcmd.c, which, in case the specified pathname does not contain forward slashes, searches $PATH for the pathname. In case pathname contains forward slashes, search_for_command() does not perform this lookup. In case search_for_command() does not return a command, execute_disk_command() will fail with the command not found internal error.

The second error originates from the shell_execve() function, also defined in execute_command.c. At this point in your scenario,search_for_command() would have returned successfully because there would not have been any lookup needed, and execute_disk_command() has called shell_execve(), which in turn performs the execve() system call. This fails, because the file execve() attempts to execute does not exist, and execve() indicates this by setting errno appropriately. When execve() fails, shell_execve() uses strerror() to report the corresponding file error message (No such file or directory) and exits the shell immediately on the error.

share|improve this answer
    
Awesome, this is what I wanted to know. Now I think that I can edit bash source code and make an URL to be opened from terminal only by typing its name. –  Radu Rădeanu Oct 25 '13 at 8:23

The / character is a pathname separator. The shell therefore assumes you're specifying a relative path to a command named unix.stackexchange.com under a directory named http:. The shell fails to find the directory or the file, and says so. You would get the same error message if you attempted to run a command named (for example): foo/bar/grill/snorklewacker.

On the other hand, if you enter a bare command name with no pathname separator, then the shell checks the list of directories in the PATH environment variable, in the order in which they appear, to see if they contain the program. Thus, if you were to enter unix.stackexchange.com at a prompt on my system, the shell would conduct a search looking for:

  • /usr/local/bin/unix.stackexchange.com
  • /usr/bin/unix.stackexchange.com
  • /bin/unix.stackexchange.com
  • /usr/local/games/unix.stackexchange.com
  • /usr/games/unix.stackexchange.com

If all of those searches fail, then you get the error command not found.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.