Is it possible to catch the command not found error?

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)

4

1 Answer

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

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like