shell

How do you process the output of a command in the shell line-by-line?


I need to process the shared library dependencies of a library from a bash script. The for command processes word-by-word:

for DEPENDENCY in `otool -L MyApplication | sed 1d`
do
    ...
done

What is the way to process the results line-by-line?


Solution

  • You should use the read command.

    otool -L MyApplication | sed 1d | \
    while read i
    do
      echo "line: " $i
    done
    

    See bashref for a description of the read builtin, and its options. You can also have a look at the following tutorial for examples of using read in conjunction with for.