You can use wc -L to report the length of the longest line in the input (excluding the newline character of a line).

$ echo 'apple' | wc -L
5

# last line not ending with newline won't be a problem
$ printf 'apple\nbanana' | wc -L
6

$ cat greeting.txt
hi there
have a nice day
$ wc -L <greeting.txt
15

If multiple files are passed, the last line summary will show the maximum length among the given inputs.

$ wc -L greeting.txt sample.txt para.txt
 15 greeting.txt
 26 sample.txt
 11 para.txt
 26 total

-L won't count non-printable characters and tabs are converted to equivalent spaces. You can use awk if these are not acceptable.

# tab characters can occupy up to 8 columns
$ printf '\t' | wc -L
8
$ printf '(\t)' | wc -L
9
$ printf '(\t)' | awk '{print length()}'
3

# non-printable characters aren't counted
$ printf '(\34)' | wc -L
2
$ printf '(\34)' | awk '{print length()}'
3

Note that the awk command in the above illustration is similar to wc -L only for single line inputs. For multiple lines, you can use the following command:

awk '{len = length(); if(len > max) max = len} END{print max}'

Multibyte characters and grapheme clusters will each be counted as 1, assuming the current locale is set appropriately:

# multibyte characters are counted as 1 each in supported locales
$ printf 'αλεπού' | wc -L
6

# grapheme cluster example
$ printf 'cag̈e' | wc -L
4

# non-supported locales can cause them to be treated as non-printable
$ printf 'αλεπού' | LC_ALL=C wc -L
0

Video demo:


info See also my Command line text processing with GNU Coreutils ebook.