OSX terminal string length

bash

Solution

echo "foo" | wc -c

This counts the number of characters on stdin. Note that this will return `4` since `echo` adds a newline at the end. Use `printf` or `echo -n` to avoid the added newline.

printf "foo" | wc -c

Problem

Is it possible in OSX's terminal to get the length of a string? Eg: ``` $ echo "foo".length ``` Or ``` $ echo (cat file.txt).length ```

Original source