这个循环将显示我想做的事情,但是如果我从中删除 echo
,它实际上不会删除任何东西:
history | grep ":[0-5][0-9] ls *$" | cut -c1-5 |
while read id; do
echo history -d $id
done
我添加了缩进以使其更具可读性,但我将其作为单行代码从命令行运行。
我设置了 HISTTIMEFORMAT
,因此 grep 会找到秒数,然后是 ls
,然后是任意数量的空格。从本质上讲,它是在历史中寻找任何只是 ls
的东西。
这是在 Ubuntu 14.04.3 LTS
上使用 bash 4.3.11
最佳答案
history -d
从内存中的历史记录中删除一个条目,并且您正在管道诱导的子 shell 中运行它。这意味着您要从子 shell 的历史记录中删除一个历史条目,而不是当前 shell 的历史记录。
使用流程替换来提供循环:
while read id; do
history -d "$id"
done < <(history | grep ":[0-5][0-9] ls *$" | cut -c1-5)
或者,如果您的 bash
版本足够新,请使用 lastpipe
选项来确保您的 while
循环在当前 shell 中执行.
shopt -s lastpipe
history | grep ":[0-5][0-9] ls *$" | cut -c1-5 |
while read id; do
echo history -d $id
done
关于linux - 为什么我不能用这个循环从 bash 历史记录中删除多个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34360534/