In a Linux shell how can I process each line of a multiline string?

Use this (it is loop of reading each line from file file)

cat file | while read -r a; do echo $a; done

where the echo $a is whatever you want to do with current line.

UPDATE: from commentators (thanks!)

If you have no file with multiple lines, but have a variable with multiple lines, use

echo "$variable" | while read -r a; do echo $a; done

UPDATE2: “read -r” is recommended to disable backslashed (\) chars interpretation (check mtraceur comments; supported in most shells). It is documented in POSIX 1003.1-2008 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html

By default, unless the -r option is specified, <backslash> shall act as an escape character. .. The following option is supported: -r – Do not treat a <backslash> character in any special way. Consider each to be part of the input line.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)