Suppose I have some output from a command (such as ls -1
):
a
b
c
d
e
...
I want to apply a command (say echo
) to each one, in turn. E.g.
echo a
echo b
echo c
echo d
echo e
...
What's the easiest way to do that in bash?
Suppose I have some output from a command (such as ls -1
):
a
b
c
d
e
...
I want to apply a command (say echo
) to each one, in turn. E.g.
echo a
echo b
echo c
echo d
echo e
...
What's the easiest way to do that in bash?
You can use a for loop:
for file in * ; do echo "$file" done
Note that if the command in question accepts multiple arguments, then using xargs is almost always more efficient as it only has to spawn the utility in question once instead of multiple times.
You can use a basic prepend operation on each line:
ls -1 | while read line ; do echo echo $line ; done
Or you can pipe the output to sed for more complex operations:
ls -1 | sed 's/^\(.*\)$/echo \1/'
It's probably easiest to use xargs
. In your case:
ls -1 | xargs -L1 echo
for s in `cmd`; do echo $s; done
If cmd has a large output:
cmd | xargs -L1 echo