Skip to content

Instantly share code, notes, and snippets.

@wajatimur
Created January 27, 2016 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wajatimur/9968043f5274362c9f4d to your computer and use it in GitHub Desktop.
Save wajatimur/9968043f5274362c9f4d to your computer and use it in GitHub Desktop.
Search command line history and run
#!/bin/bash
IFS=$'\n'
HISTFILE=~/.zsh_history
set -o history
declare -a H
HISTORY_CMD=""
function displayList {
IDX=0
for cmd in ${H[@]}; do
let IDX+=1
if [ -n "${H[$IDX]}" ]; then
echo "[$IDX] ${H[$IDX]}"
fi
done
}
function execCommand {
if [ -n "$1" ];
then
HISTORY_CMD=${H[$1]}
fi
}
H=( $(history | grep -oE '(?)\;(.*)$' | cut -c2- | grep $1 | grep -ovE '^history(.*)' | grep -ovE '^(./historun|historun)(.*)') )
if [ ${#H[@]} -gt 0 ];
then
displayList
else
echo "Sorry no matches found!"
exit
fi
read -p "Please enter your history ID, enter 'q' to cancel [q]: " HISTORY_ID
if [ $HISTORY_ID == 'q' ];
then
exit
else
execCommand $HISTORY_ID
fi
if [ -n "$HISTORY_CMD" ];
then
eval $HISTORY_CMD
else
displayList
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment