How to ensure only one file is returned by checking for newline How to ensure only one file is returned by checking for newline unix unix

How to ensure only one file is returned by checking for newline


You can use pattern matching:

[[ $FILE == *$'\n'* ]] && echo More than one line


If $str contains new line you can check it by,

if [ $(echo "$str" | wc -l) -gt 1 ];then     // perform operation if it has new lineselse     // no new lines.fi 


To refer to your example: Note that filenames could contain newlines, too.

A safe way to count files would be

find -name "pattern_*.sh" -printf '\n' | wc -c

This avoids printing the filename and prints only a newline instead.