Home > Enterprise >  How can I make Bash save unrecognized commands to a file?
How can I make Bash save unrecognized commands to a file?

Time:07-23

How is Bash simple one line syntax to check and get the invalid or unrecognized command fed in shell or terminal prompt to be saved in a file? Illustration e.g.

$ run_bash_command FILE

$ foo
bash: foo: command not found

$ bar
bash: bar: command not found

$ baz
bash: baz: command not found

$ cat FILE
foo
bar
baz

CodePudding user response:

With bash version >= 4.0 I suggest to add this function to ~/.bashrc and start a new session:

command_not_found_handle() {
  echo "bash: $1: command not found" >&2;
  echo "$@" >> /tmp/file.txt;
}
  • Related