Skip to content

Instantly share code, notes, and snippets.

@tokland
Last active June 22, 2024 15:33
Show Gist options
  • Save tokland/da0d9c6fbb9139c5742d2100b734c839 to your computer and use it in GitHub Desktop.
Save tokland/da0d9c6fbb9139c5742d2100b734c839 to your computer and use it in GitHub Desktop.
Fast search of commands in shell history
#!/bin/bash
# Search commands in history
set -e -u -o pipefail
if [ "$#" -lt 1 ]; then
echo "Usage: $0 wordToMatch anotherWordToMatch -skipIfWordMatches +wordToMatchCaseSensitive ..."
exit 1
fi
command="cat ~/.bash_history"
for word in "$@"; do
if [[ $word == -* ]]; then
command="$command | grep -vai \"${word#-}\""
elif [[ $word == +* ]]; then
command="$command | grep -a \"${word#+}\""
else
command="$command | grep -ai \"$word\""
fi
done
# shellcheck disable=SC2086
eval $command | uniq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment