koalaman/shellcheck

Ignore SC2329 for command_not_found_handle()

Opened this issue · 0 comments

For new checks and feature suggestions

Here's a snippet or screenshot that shows a potential problem:

#!/bin/bash
command_not_found_handle() {
  echo hi
}
not_exists

Here's what shellcheck currently says:

command_not_found_handle() {
^-- SC2329 (info): This function is never invoked. Check usage (or ignored if invoked indirectly).

Here's what I wanted to see:

no error

Additional info from man bash:

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 function named
command_not_found_handle.  If that function exists, it is invoked in a separate execution environment
with the original command and the original command's arguments as its arguments, and the function's exit
status becomes the exit status of that subshell.  If that function is not defined, the shell prints an
error message and returns an exit status of 127.