假设我有一些来自命令的输出(例如 ls -1
):
a
b
c
d
e
...
我想依次对每个命令应用一个命令(比如 echo
)。例如
echo a
echo b
echo c
echo d
echo e
...
在 bash 中最简单的方法是什么?
最佳答案
使用 xargs
可能是最简单的。在你的情况下:
ls -1 | xargs -L1 echo
-L
标志确保正确读取输入。来自 xargs
的手册页:
-L number
Call utility for every number non-empty lines read.
A line ending with a space continues to the next non-empty line. [...]
关于bash - 如何将 shell 命令应用于命令输出的每一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2711001/