BASH 如何将 rm -vri 的输出保存到 var

BASH How to save the output of rm -vri to a var

如何将此命令的输出保存到 var 中并仍然删除文件?

rm -vri files | wc -l
$ touch file1 file2
$ out=$(rm -vri file1 file2 | wc -l)
rm: remove regular empty file ‘file1’? y
rm: remove regular empty file ‘file2’? y
$ echo $out
2

如您所见,使用正常的 var=$(command) 方法是可行的。

这在 运行 来自脚本时也有效。显然,您确实需要提供输入(例如 运行 来自 cron 的输入将不起作用)。