Currently I am using $HISTIGNORE to ignore some commands and not save them in bash history. But current behaviour is if I am running a terminal I executed a command which is in $HISTIGNORE then on hitting up arrow it takes me to last saved command but I want to have these ignored commands available in current session and delete them only after the session is closed.


You can use sed to delete lines from your ~/.bash_history file, e.g.

sed -i -e '/ apt /d' -e '/^xterm/d' ~/.bash_history

to delete every line containing “ apt ” and every line beginning with “xterm”.

If you want to perform the deletion every time you open a new terminal, add the sed line to your ~/.bashrc file. You can also use cron (crontab -e) to automate the cleanup, e.g. with the line

@hourly sed -i -e '/ apt /d' -e '/^xterm/d' ~/.bash_history

to perform the cleanup hourly.

| improve this answer | |
  • Thanks man its really helpful but, Can I do the deletion of lines when terminal is killed and only for the commands typed in current session of terminal? – ksmukta Sep 17 '17 at 5:10
  • @ksmukta I searched for a way to delete the lines automatically when a terminal is closed, but I couldn't find anything useful in this case. You can do the deletion whenever you want and it will delete all matched lines in the file – what's the use of restrict it to the current session's commands? – dessert Sep 17 '17 at 5:31
  • 1
    @ksmukta You may find the Bash History Builtins chapter of the Bash Reference Manual interesting. – dessert Sep 17 '17 at 6:26

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.