Last active
June 22, 2024 15:33
-
-
Save tokland/da0d9c6fbb9139c5742d2100b734c839 to your computer and use it in GitHub Desktop.
Fast search of commands in shell history
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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