该循环将显示我想要执行的操作,但是如果我echo
从中删除,则实际上不会删除任何内容:
history | grep ":[0-5][0-9] ls *$" | cut -c1-5 |
while read id; do
echo history -d $id
done
我添加了缩进以使其更具可读性,但是我从命令行将其作为单行代码运行。
我已经HISTTIMEFORMAT
设置好,因此grep可以找到秒数,ls
然后是任意数量的空格。从本质上讲,它在历史上发现的只是一个东西ls
。
这bash 4.3.11
用于Ubuntu 14.04.3 LTS
history -d
从内存历史记录中删除一个条目,然后在管道引起的子外壳中运行它。这意味着您要从子外壳程序的历史记录中删除历史记录条目,而不是当前外壳程序的历史记录。
使用流程替换来填充循环:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句