Is It Possible to Catch the Command Not Found Error?

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

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

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.