bash stores a user's command line history in a plain text file "~/.bash_history".

Assuming only one user is using a single terminal, are there any bad side effects of opening "~/.bash_history" in a terminal-based text editor, (or sed, et al), and saving it while that user's instance of bash is running?


If it is a single terminal, with only one user using it, no there is no problem.

Bash history can be used as log, but it is only for the user, so it is not safe for security checkups, if you want a more safe log you must configure rsyslog, and consider putting your log in another partition with replication and log rotate.

For example try to log in as some user, do some commands and run kill -9 $$ you will see that logs are not registered in bash history.

To configure rsyslogd the basic you want to do is (in debian for instance):

cat >/etc/rsyslog.d/bash.conf
local6.*    /var/log/commands.log
[CTRL+D for save the file)


editor /etc/bash.bashrc

And inside (after the first lines)

export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$SSH_CLIENT $(whoami) [$$]: $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" ) [$RETRN_VAL]"'

The bash history file is just a simple text file keeping a log of historical commands processed on the users account.

There should be no effect, unless you have scripts that process this file for some reason.

For example - I just deleted my bash history and exited the terminal, then logged back in. Bash created a new history file.

Hint: If you want to prevent a command from being logged in your history, precede it with a space.

  • Re "no effect": please clarify whether is this means no effect whatsoever, (the edit will fail), or no bad side-effects. – agc Aug 23 '18 at 18:40
  • Also, writing to a file and deleting a file are, from the system’s point of view, substantially different operations.  I agree with your overall statement, but your argument (‘‘I deleted the file and I didn’t notice any problem, so editing the file should be OK too’’) is not valid. – G-Man Aug 23 '18 at 19:00

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.