Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSX terminal string length

Tags:

bash

Is it possible in OSX's terminal to get the length of a string? Eg:

$ echo "foo".length

Or

$ echo (cat file.txt).length
like image 274
Ahmed Nuaman Avatar asked Jan 13 '14 19:01

Ahmed Nuaman


Video Answer


2 Answers

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
like image 169
John Kugelman Avatar answered Oct 02 '22 14:10

John Kugelman


Try with this:

var=hello; echo ${#var}
like image 24
dmarin Avatar answered Oct 02 '22 15:10

dmarin