"stdout manipulation"
Usually called output parsing. It does happen a quite lot and yes it is problematic for the reason you mention.
In this particular situation I would recommend pgrep
command, which is part of procps
package (on Ubuntu). Mmm.. not sure about portability here though.
number_of_processes=$(pgrep -c ...)
Yep ... -c
flag of pgrep
means something different on BSD ... https://www.freebsd.org/cgi/man.cgi?query=pgrep
Side notes
grep ... | wc -l
can be grep -c
, at least on some platforms.