Live data from Hacker News

How and Why to Log Your Bash History

spin.atomicobject.com

11–20 of 139 posts

Re: How and Why to Log Your Bash History

#11
post #3
post #2

Well given that we copy and paste a lot, by mistake there can be credentials and sensitive data in the bash history... Moreover, bash history doesn't work nicely across different terminal tabs or computers. Does anybody know a tool providing better command line history?

For better history across different terminal (on the same computer) you may want to try zsh

For better pretty much everything, one may wish to try zsh. It's pretty much a strict superset of bash, but better.

There is one aspect in which it's significantly worse, though: it's fiendishly difficult to configure and understand. I just use configs provided by others, which is not ideal but works.

Re: How and Why to Log Your Bash History

#12
post #8

Why logging to a file when you could just set the HITSIZE variable in your .bashrc ? (plus this will give you ctrl+r search which is a must)

You would also need to unset HISTFILESIZE, export PROMPT_COMMAND="history -a", export HISTTIMEFORMAT="%d/%m/%y %T ", and set up log rotation to get the behavior listed in the article.

Re: How and Why to Log Your Bash History

#14

This looks like quite a dirty hack, but I'll try it. I wish bash history wasn't so broken. I suppose once upon a time it was ok to assume people only had one shell session at a time.

Wouldn't the ideal solution not be a component of a simple file writer, but rather, a buffered writer? A command gets executed, thrown into buffer. A wait process in bash checks buffer, and then writes to .bash_history?

Re: How and Why to Log Your Bash History

#15
Possibly a good opportunity to ask:

On Mac OS X and iTerm2 I've noticed a thing where pasting in commands means they don't get added to the history and using up arrow or search to find them doesn't work. Anyone seen that? It's one of those things that annoys me every time it happens, but I'm always in the middle of a task when it occurs so I never get a chance to figure it out.

Re: How and Why to Log Your Bash History

#16

Possibly a good opportunity to ask: On Mac OS X and iTerm2 I've noticed a thing where pasting in commands means they don't get added to the history and using up arrow or search to find them doesn't work. Anyone seen that? It's one of those things that annoys me every time it happens, but I'm always in the middle of a task when it occurs so I never get a chance to figure it out.

I've just tried and I can't reproduce. It might be because you're copying a leading space: if a command is prefixed by white space it doesn't get saved in the history.

Re: How and Why to Log Your Bash History

#18

Possibly a good opportunity to ask: On Mac OS X and iTerm2 I've noticed a thing where pasting in commands means they don't get added to the history and using up arrow or search to find them doesn't work. Anyone seen that? It's one of those things that annoys me every time it happens, but I'm always in the middle of a task when it occurs so I never get a chance to figure it out.

Could it be that you're accidentally adding/copying a space at the beginning of the command?

I know Linux BASH will ignore the command from history if that's the case, and I think that OSX is similar.

Re: How and Why to Log Your Bash History

#19
post #9
post #4

This is useful if you are developing locally, but what if a lot of your command line usage is on remote servers that you have SSH'd into? I believe iTerm2 has some support for logging remote commands, but I'm not sure it quite does the trick. Anybody know of another tool that will log both local and remote commands?

Another problem for me is the ephemeral nature of many of the boxes I work on - Vagrant boxes, spot instances on AWS. I suppose I could do this and then export the bash history as part of the teardown process. At the moment, I get around this by storing as much as I can in searchable scratch files, but this relies on me knowing what to copy. I'm sure there's a better way.

Work through ansible and manage your local ansible.log however you wish.

Re: How and Why to Log Your Bash History

#20
I do something very similar, but without the prompt settings. I have settings in .bashrc[0] to have the history file based on date. I then use fzf[1] (fzf-tmux is great) and a grep-like tool(sift[2]) to use for ctrl-r that fuzzy-searches history and orders by usage frequency[3]. This way I can easily search for the command I'm thinking of fairly quickly. Particularly useful for those times I want to run a command again that was quite long or had more than a couple options/flags.

---

[0] HISTFILE="${HOME}/Sync/Dotfiles/history/$(date -u +%Y-%m-%d.%H.%M.%S)_${HOSTNAME_SHORT}_$$"; export HISTFILE

[1] https://github.com/junegunn/fzf

[2] https://sift-tool.org/

[3] __fzf_history__() {

sift --no-color -e "^[^#]" --files "_${HOSTNAME_SHORT}_" -N --no-filename $HOME/Sync/Dotfiles/history | sort | uniq -c | sort | $(__fzfcmd) +s --tac +m -n1..,.. --tiebreak=index --toggle-sort=ctrl-r | sed "s/ [0-9] *//"

}

Post reply on HN