I have a problem with
if [[ "$1" =~ any$ ]]; then
bash: no such file or directory: whatever/any
if [[ "$1" =~ /any$ ]]; then
if [[ "$1" =~ \/any$ ]]; then
command_not_found_handle (no trailing
r) is only invoked after doing a search through the PATH for a given command.
No such search occurs when the user is passing an explicit path to a command, which is how anything containing a
/ is interpreted.
To quote the relevant documentation, with emphasis added:
- If the name is neither a shell function nor a builtin, and contains no slashes, Bash searches each element of
$PATHfor a directory containing an executable file by that name. Bash uses a hash table to remember the full pathnames of executable files to avoid multiple
PATHsearches (see the description of hash in Bourne Shell Builtins). A full search of the directories in
$PATHis performed only if the command is not found in the hash table. If the search is unsuccessful, the shell searches for a defined shell function 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.
The entire paragraph of documentation is relevant only in the set of conditions set out at the beginning: A command must not be a shell function, must not be a builtin, and must not contain slashes.