Skip to content

Instantly share code, notes, and snippets.

@sinewalker
Created March 16, 2018 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinewalker/1a628deaa26fbf3b9909a8f182fc6fdc to your computer and use it in GitHub Desktop.
Save sinewalker/1a628deaa26fbf3b9909a8f182fc6fdc to your computer and use it in GitHub Desktop.
Delete last command from shell history, AND the fact that you deleted it, without HISTCONTROL="ignorespace" having been set
history -d $((${HISTCMD}-1));history -d $((${HISTCMD}-1))
@sinewalker
Copy link
Author

So, you're on a server with shared Unix accounts, and you accidentally enter a command with a secret in it (a password, say). Whoops!

Then you realize your gaff, haven't got my handy alias in this shell's environment; and you need to remove it from the shell history immediately. You do this:

$ history -d $((${HISTCMD}-1))

Phew!

Oh, but now someone looking in the history will see that and wonder "What was deleted?". To avoid that, if you've set HISTCONTROL="ignorespace" then starting your commands with a space automatically prevents that command being written to history.

You could have done this in your original gaff too, btw, if you knew you were having to enter a secret.

$  history -d $((${HISTCMD}-1))

(note the space at the front).

Anyway, suppose the HISTCONTROL isn't set? Or you're not allowed to by Policy? What then?

Delete the deletion too, but do it in a single line:

$ history -d $((${HISTCMD}-1));history -d $((${HISTCMD}-1))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment