I have a FreeBSD 9.3 RELEASE server and I am trying to write a shell script using sh.I am trying to get the length of a variable that is passed to a function in a shell script, like so:#!/bin/shmyfunc() {  varlength=${#1}  echo $varlength}test=aaaaa aaaaamyfunc $testThe above script should return 11, which is the length of the test variable, but ${#1} does not seem to work. I have also tried many other ways to accomplish that but i can't figure it out.varlength=$(expr ${#1})  does not workvarlength=$(${#1})  does not workvarlength=$(expr \( X$1 : .* \) - 1)  does not workvarlength=$({#1})  does not workand many many other attempts I made failed.
Length of argument inside a function of an sh script
shell;string;variable
because $test contains whitespace, when you saymyfunc $test # without quotesyour function receives > 1 argument. myfunc receives 2 arguments here, aaaaa and  aaaaa.You want this:myfunc $test # with quotesRule of thumb: always quote your $variables unless you know exactly when and why not to.