Rajarshi Das
Rajarshi Das

Reputation: 12320

How to remove any commands that begins with "echo" from history

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

Upvotes: 0

Views: 233

Answers (3)

Piotr Henryk Dabrowski
Piotr Henryk Dabrowski

Reputation: 901

history -w
sed -i '/^echo.*/d' ~/.bash_history
history -c
history -r

Upvotes: 0

Philippe
Philippe

Reputation: 26437

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

Upvotes: 1

user1934428
user1934428

Reputation: 22225

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

Upvotes: 1

Related Questions