Skip to content

Instantly share code, notes, and snippets.

@masato9000
Created May 31, 2021 03:12
Show Gist options
  • Save masato9000/09e11145ead8a0864476d992b9e19c6b to your computer and use it in GitHub Desktop.
Save masato9000/09e11145ead8a0864476d992b9e19c6b to your computer and use it in GitHub Desktop.
# Some history manipulations for Korn shells. Potential side effects unknown.
# The idea is that I don't want to commit to having a separate history file
# for EVERY interactive shell or deal with merging per-instance histories back
# into the main history, but sometimes it's nice to have a different history
# in the current shell instance (tty, xterm, whatever)
# history+ Increment the command history file version
# history- Decrement the command history file version
# historyoff Enter subshell with no command history
# historyon Cancel historyoff (exit subshell)
# Relies on a default HISTFILE variable being set.
# Enable by putting a non-empty string in the initial test.
if [ -n "history" ] && [ -n "$HISTFILE" ]; then
export THISSHELL="$0"
if [ -z "$BASEHISTFILE" ]; then
export BASEHISTFILE=$HISTFILE
fi
if [ -z "$HISTFILENUM" ]; then
export HISTFILENUM=0
elif [ $HISTFILENUM -gt 0 ]; then
export HISTFILE=$BASEHISTFILE.$HISTFILENUM
elif [ $HISTFILENUM -lt 0 ]; then
export HISTFILE=/dev/null
fi
history+ () {
if [ $HISTFILENUM -lt 0 ]; then
echo command history disabled. Not updating HISTFILE
else
export HISTFILENUM=$(($HISTFILENUM + 1))
export HISTFILE=$BASEHISTFILE.$HISTFILENUM
echo Using command history in $HISTFILE
fi
}
history- () {
if [ $HISTFILENUM -lt 0 ]; then
echo command history disabled. Not updating HISTFILE
elif [ $HISTFILENUM -eq 0 ]; then
echo Already using default history file. Not updating HISTFILE
else
export HISTFILENUM=$(($HISTFILENUM - 1))
if [ $HISTFILENUM -eq 0 ]; then
export HISTFILE=$BASEHISTFILE
else
export HISTFILE=$BASEHISTFILE.$HISTFILENUM
fi
echo Using command history in $HISTFILE
fi
}
historyoff () {
if [ $HISTFILENUM -ge 0 ]; then
echo Starting new subshell without command history
echo Type \"historyon\" or \"exit\" to resume regular command history func
tion
echo NOTE: This does NOT disable in-memory command history
HISTFILENUM=-1 $THISSHELL
echo Command history resumed. HISTFILE=\"$HISTFILE\"
fi
}
historyon () {
[ $HISTFILENUM -lt 0 ] && exit
}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment