0

I have tried the below

history -d $(history | grep "echo.*" |awk '{print $1}')

But it is not deleting all the commands from the history with echo

I want to delete any commands start with echo

like

echo "mamam"
echoaaa
echo "hello"
echooooo

3 Answers 3

1

You can use this to remove echo entries :

for d in $(history | grep -Po '^\s*\K(\d+)(?= +echo)' | sort -nr); do history -d $d; done
1

I would do a

 history -d $(history | grep -E "^ *[0-9]+ *echo" | awk '{print $1})

The history command produces one column of event number, followed by the command. We need to match an echo, which is following such a event number. The awk then prints just the event number.

An alternative without reverting to awk would be:

history -d $(history | grep -E "^ *[0-9]+ *echo" | grep -Eow '[0-9]+)
2
  • not working .....history -d $(history | grep "^echo") -bash: history: -d: option requires an argument history: usage: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...] Commented Dec 10, 2021 at 10:12
  • @RajarshiDas : Fixed - please try it. Commented Dec 10, 2021 at 10:18
0
history -w
sed -i '/^echo.*/d' ~/.bash_history
history -c
history -r

Not the answer you're looking for? Browse other questions tagged or ask your own question.