5

I want to make a custom "command not found" prompt for the terminal like this one. I have the template set up:

   ___________________________________________
  / I see you're trying to use the terminal … \
 | Command not found:                        |
 |                                           |
 |  xxx                                      |
 |                                           |
  _____ ____________________________________/
        v
       ╭─╮
       ⌾ ⌾
       │▕│
       ╰─╯

I want the "xxx" to be replaced by the incorrect command. How could I achieve this?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Cameron R.
  • 51
  • 2

1 Answers1

5

When bash can't find a command name, it executes the function command_not_found_handle with the original command and its arguments as arguments. So define your own. This goes into your ~/.bashrc. Make sure to print to standard error, not standard output, and to return the same exit status, otherwise this could be disruptive to shell script snippets executed inside that bash instance.

command_not_found_handle () {
  local cmd
  printf -v cmd "%-40s" "${cmd:0:40}"
  cat >&2 <<EOF
   ___________________________________________
  / I see you're trying to use the terminal … \
 | Command not found:                        |
 |                                           |
 |  $cmd |
 |                                           |
  _____ ____________________________________/
        v
       ╭─╮
       ⌾ ⌾
       │▕│
       ╰─╯

EOF
  return 127
}
Evgeny Vereshchagin
  • 5,286
  • 4
  • 35
  • 43
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175