4 ms·
Modern shells are powerful enough to help you remember, if you learn to configure them appropriately. My histories are always saved because each shell instance
by jcynix 6y ago
Modern shells are powerful enough to help you remember, if you learn to configure them appropriately. My histories are always saved because each shell instance gets its own HISTFILE, like so:
export HISTFILE=$HOME/.history/${TTY##\*/}.$SHLVL
As I use different terminal windows for different tasks, this keeps history files rather concise thematically.
And I let the shell add timestamps too, so I can grep for entries produced during a certain time span:
zsh:
setopt EXTENDEDHISTORY # add timestamps
bash:
HISTTIMEFORMAT="%F %T "
I write perl or shell script files, of course, if it's more than some a handful of lines.
- deleted 6y ago[deleted]
- amelius 6y agoCan you also add the working directory in which each command was run?
- jcynix 6y agoYou could deduce the working directory from the sequence of commands in your history file, so the working directory is implicitly contained in the history file. I don't remember an option to save the working directory explicitly. But zsh has a number of history related commands, which can be used to execute shell functions before and after each command. So you could use these to write your own special history file, even one per directory if need. Example: zshaddhistory () { echo "$PWD -- $1" >> $MY_HISTFILE } This shell function defined here will run when the command has been read but just before it will be executed. The argument $1 contains the command line to be executed.
- amelius 6y ago> You could deduce the working directory from the sequence of commands in your history file, so the working directory is implicitly contained in the history file. I didn't think of that, but I don't think it's always possible. E.g. suppose you run "source somefile", and somefile contains a cd command. Unfortunately I'm using bash mostly, so I'm afraid the suggestion you gave for zsh doesn't work for me. By the way, another thing I'm interested in is how people manage their history files over multiple machines.
- aidenn0 6y agoDoes bash have an option to write out the history before running the command? Usually history is written in PROMPT_COMMAND, which runs after the command completes.
- jcynix 6y agoI don't know about bash, but zsh allows you to define a shell function "zshaddhistory" which is running right before a command is executed.
- joombaga 6y agoNot exactly, but there's $PS0 if you have bash 4.4+. https://stromberg.dnsalias.org/~strombrg/PS0-prompt/ https://stromberg.dnsalias.org/~strombrg/PS0-prompt/ (Thanks Dan!)
- aidenn0 6y agoThanks! I must be blind, I was looking in the bash man page right around PS1 and somehow didn't see this, but it's definitely there.
- bandie91 6y agoyes, using the DEBUG trap.
- misnome 6y agoI do something similar, I gave up on trying to get the bash history to work properly and so just have this: echo "$HOSTNAME $$ $(date "+%Y-%m-%dT%H:%M:%S%z") $*" >> ~/.fullhistory It's attached to the preexec hook of https://github.com/rcaloras/bash-preexec https://github.com/rcaloras/bash-preexec, so is run before every command. This means that everything goes into one easily-greppable file, but is still separable by PID/host machine - since my work has me walking around a large facility, often I'll remember where I was when I did something but not exactly when, so can narrow down by machine.