Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sometimes I misunderstand the syntax of a command:

# mysql -d test
mysql: unknown option '-d'
# echo $?

I try again and get it right:

# mysql --database test
Welcome to the MySQL monitor.
mysql >

How do I prevent the first command, with error code different than 0, to enter the history?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

I don't think you really want that. My usual workflow goes like this:

  • Type a command
  • Run it
  • Notice it failing
  • Press UP key
  • Edit the command
  • Run it again

Now, if the failed command weren't saved into history I couldn't get it easily back to fix and run again.

share|improve this answer
+1 for getting the primary point of hihstory! – ams Jul 4 '12 at 21:11
I guess a better design would be a session history and a permanent history. Thanks! – Adam Matan Jul 5 '12 at 5:22

The only way I can think of to do this would be to use history -d in $PROMPT_COMMAND. The problem with this or any approach is that it's impossible to tell if a command exited with an error or completed successfully with a non-zero exit code.

$ grep non_existent_string from_file_that_exists
$ echo $?
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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