So, if you're on Posix, and you type something that isn't a command, it gives you a "command not found" error.

> ggg
-bash: ggg: command not found

Is there a way to intercept this and have it trigger a different command? What about changing the text.

(I am using a Mac… if that matters, but I also use Ubuntu and varieties of RHEL)

  • Have a look on this post too: unix.stackexchange.com/questions/104579/… – Pillsbury IT Doughboy Oct 20 '17 at 20:00
  • @Facebook That looks like a great answer. Almost "this should be marked duplicate" level. – cwallenpoole Oct 20 '17 at 20:11
  • If it's not on SuperUser it is not duplicate so leave for now until others see and chime in. – Pillsbury IT Doughboy Oct 20 '17 at 20:12
  • What do you think about the double pipe method from the other answer? Maybe that and have command 2 or the error command grep to find a specific string and only run another command if that string (e.g. "command not found") value is true? – Pillsbury IT Doughboy Oct 20 '17 at 20:14

Bash doesn't have a Try/Catch/Except, but you can use || like this. If the second command will only run, if the first one fails. Thats what the double pipe is doing here. And thats for any kind of error.

gggg || gggg2

where gggg2 is your other command

  • So with this solution he could potentially run <command 1> || <error command> with all applicable commands and that would ensure that the error command is only run in the event that the first command fails. – Pillsbury IT Doughboy Oct 20 '17 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.