4

I'm trying to setup the command_not_found_handle for URL links,

%> command_not_found_handle() { echo "OK"; }
%> aaaaa
OK
%> http://www.google.com
bash: http://www.google.com: No such file or directory

So it works for normal input, but not URL links, any ideas?

1
  • command_not_found_handle only triggers for PATH lookups, and a URL doesn't trigger a PATH lookup. I think you'd have to patch bash (though maybe you can do something with a debug trap). Nov 17, 2013 at 20:52

1 Answer 1

6

URLs are treated as containing subdirectories. E.g. see this:

$ command_not_found_handle() { echo "OK"; }
$ aaa
OK
$ aaa/bbb
bash: aaa/bbb: No such file or directory
$ 

That is, it works when you run a command, but then when you run a command with a slash, it likely tries to do bbb in folder aaa. The same happens with URLs - it considers it running the command www.google.com within the directory http:.

From the bash man page:

If the name is neither a shell function nor a builtin, and contains no slashes, bash searches each element of the PATH for a directory containing an executable file by that name. Bash uses a hash table to remember the full pathnames of executable files (see hash under SHELL BUILTIN COMMANDS below). A full search of the directories in PATH is performed only if the command is not found in the hash table. If the search is unsuccessful, the shell searches for a defined shell func‐ tion named command_not_found_handle. If that function exists, it is invoked with the original command and the original command's arguments as its arguments, and the function's exit status becomes the exit status of the shell. If that function is not defined, the shell prints an error message and returns an exit status of 127.

Emphasis mine.

2
  • so what's the solution?
    – thewpfguy
    May 18, 2017 at 12:38
  • @thewpfguy Except what Gilles suggested, I don't think there's a solution that supports using slashes. May 24, 2017 at 0:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .