-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh
More file actions
executable file
·37 lines (29 loc) · 1.1 KB
/
h
File metadata and controls
executable file
·37 lines (29 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env bash
#
# find_history.sh — search your Bash history for a pattern
#
# Usage:
# ./find_history.sh <pattern>
#
# The script looks for <pattern> (arg 1, required) in your Bash history file
# and prints matching commands with line numbers and color‐highlighted matches.
set -euo pipefail
# ---- argument handling ------------------------------------------------------
if [[ $# -ne 1 ]]; then
echo "Usage: $(basename "$0") <search_pattern>" >&2
exit 1
fi
pattern=$1
# ---- locate the history file ------------------------------------------------
histfile=${HISTFILE:-"$HOME/.bash_history"}
if [[ ! -f $histfile ]]; then
echo "Error: history file '$histfile' not found." >&2
exit 1
fi
# ---- search & display -------------------------------------------------------
# -F : fixed‑string search (faster, avoids unintended regex interpretation)
# -n : show line numbers
# --color=auto : highlight matches when stdout is a terminal
if ! grep --color=always -n -F -- "$pattern" "$histfile" | sed 's/^[^:]*:[[:space:]]*//' ; then
printf 'No commands in history matching: %s\n' "$pattern" >&2
fi