bash - 如何将 shell 命令应用于命令输出的每一行?

标签 bash

假设我有一些来自命令的输出(例如 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/

相关文章:

bash - 如何一次退出多个嵌套的shell?

linux - 使用查找,执行命令并输出到具有原始文件名和附加扩展名的新文件

linux - 如何使用 bash 确保 user & pass 在 curl 中是正确的

bash - 引用当前命令的先前参数

bash - 这个用于命名 iTerm 选项卡的脚本如何工作?

bash - 如何在 awk 中匹配连字符 (-) 和井号 (#)?

bash - 在 Bash 中交替合并两个文件的内容

bash - 我无法从索引数组中 Ping IP,但可以使用关联数组

linux - 在 linux 中创建命令

linux - 尝试在传递两个参数时调用脚本