Live data from Hacker News

How and Why to Log Your Bash History

spin.atomicobject.com

91–100 of 139 posts

Re: How and Why to Log Your Bash History

#91
post #58

I don't get it, isn't your bash history already logged to `.bash_history`? What's the point of using PROMPT_COMMAND to send it in addition to another file?

Excellent question! Multiple tabs will squash each other, losing history from Tab B between when tab A was opened and when tab A was closed. It's actually an insecure default - 'insecure' as in, it's a minor form of data loss. Then again, so is bash launching scripts without pipefail, variable expansion fail, etc. It'd be worth fixing all this stuff in the next major bash version.

cf.

  histappend

Re: How and Why to Log Your Bash History

#92
post #34

I go one big step further than this and log everything that comes across the screen. One time it saved me from a crontab -r that wiped out a 100+ line crontab. I had viewed it recently so I just copied it out of my history. On a day to day basis it's more about looking up old queries I typed out, the results of those queries at that time, bash commands and their results, the state of a file I edited at a certain time…

This could be done using

  script
as well.

Re: How and Why to Log Your Bash History

#94
post #58

I don't get it, isn't your bash history already logged to `.bash_history`? What's the point of using PROMPT_COMMAND to send it in addition to another file?

Excellent question! Multiple tabs will squash each other, losing history from Tab B between when tab A was opened and when tab A was closed. It's actually an insecure default - 'insecure' as in, it's a minor form of data loss. Then again, so is bash launching scripts without pipefail, variable expansion fail, etc. It'd be worth fixing all this stuff in the next major bash version.

If you can use it, zsh does not have that issue. Plus a bunch of other goodies. It's been a while since I've last used bash.

Re: How and Why to Log Your Bash History

#95
Use screen instead. Ctrl-a and "H" to enable and the same to disable. Log is saved in user directory as screen.

That way I have control of when I log and what gets logged. And way easier to find than hunting for dot files.

Re: How and Why to Log Your Bash History

#96
post #86

I do the opposite and maybe weird - I disable history, and keep the history file contents hand curated. Whenever I come up/across/use a command I'd like to keep for future or use frequently, it gets appended to the history file.

Do you use the fc command (shell builtin in the case of bash) to access the history? Or just up-arrow to scroll through the options?

Re: How and Why to Log Your Bash History

#97
post #86

I do the opposite and maybe weird - I disable history, and keep the history file contents hand curated. Whenever I come up/across/use a command I'd like to keep for future or use frequently, it gets appended to the history file.

Do you have an alias or something else that facilitates that, or do you just do it manually?

Re: How and Why to Log Your Bash History

#98
post #95

Use screen instead. Ctrl-a and "H" to enable and the same to disable. Log is saved in user directory as screen. That way I have control of when I log and what gets logged. And way easier to find than hunting for dot files.

Yours is a good additional idea, but it is in no way a substitute. You might want to look at some history you forgot to log. Having intentional logging on top of comprehensive logging is like markup metadata, very handy too.

it makes me sad when everybody doesn't see the same things the same way. I'm just waiting now for systemd to disable bash history in favor of logging in binary form all mouse rolls intermixed with key clicks till it wipes it all out when you log out in case yours is a student account on a shared university machine. Don't worry, a GUI editor for it all is on the wishlist.

Re: How and Why to Log Your Bash History

#99
post #95

Use screen instead. Ctrl-a and "H" to enable and the same to disable. Log is saved in user directory as screen. That way I have control of when I log and what gets logged. And way easier to find than hunting for dot files.

Yours is a good additional idea, but it is in no way a substitute. You might want to look at some history you forgot to log. Having intentional logging on top of comprehensive logging is like markup metadata, very handy too. it makes me sad when everybody doesn't see the same things the same way. I'm just waiting now for systemd to disable bash history in favor of logging in binary form all mouse rolls intermixed wit…

6 of one, half dozen of another. It really depends on how you see things. For security reasons we've disabled logging. And users don't access servers at the shell anyway so no need for it there. We log app usage in app.

I find screen logging a bit more useful than shell logging. Shell only records the commands sent and not the result. Screen records everything including interactive like nano.

Re: How and Why to Log Your Bash History

#100
post #24

Earlier quoted context omitted.

I'm curious as to why you consider it so broken. As far as I can tell, bash deals with multiple shell sessions exactly how I'd expect (and want) it to: an individual terminal doesn't have access to the live session from other terminals, but when you close it it appends it's session to the shared history file, so each session ends up a continuous block in that file.

I don't know what it does exactly, but it always seems to lose history after a reboot. Either the last shell session to close overwrites the others, or they fail to save the history if not closed cleanly enough.

Just to check, you do have the histappend shopt set, right?
Post reply on HN