Home > Software engineering >  How to remove any commands that begins with "echo" from history
How to remove any commands that begins with "echo" from history

Time:12-11

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

CodePudding user response:

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] )

CodePudding user response:

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
  •  Tags:  
  • bash
  • Related