When running some BASH scripts I had in my Linux box I noticed that expr length gives syntax error.

Manual pages doesn't seems to include any information about the length operator. Is it missing in Mac OS X?

What other cross-platform alternative can I use?

2 Answers 2


Instead of expr length 'keyword' you can use awk. An example would look like this echo 'keyword' | awk '{ print length }'

bounty:~ napcae$ echo 'keyword' | awk '{ print length }'  

Could you share the portion of the script that does not work on your Mac OS X? exprin general adheres to IEEE Std 1003.1-2008 (POSIX.1) and is available on all Unix Systems with the same semantics.

If you share some more information regarding your bash script, I am sure we can help. Your posted example of expr length, albeit being pretty useless per se, runs perfectly fine in my Mac OS X Mavericks Shell:

~$ expr length

Not sure what you have tried to achieve, but I could imagine your were trying to get the length of a string?

~$ a="non sequitur"
~$ expr \( "X$a" : ".*" \) - 1
~$ echo ${#a}

With regard to cross-platform, it really depends on what you are trying to accomplish with your bash script. Bash by itself is pretty cross-platform.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .