I try to test the carriage return in a variable.
$ LENGTH=`expr $VARIABLE : ".*"` will return the length of the variable. But this doesn't work if $VARIABLE has zero length.
Any help will be well appreciated.
Thanks in advance.
Giovanni
I try to test the carriage return in a variable.
$ LENGTH=`expr $VARIABLE : ".*"` will return the length of the variable. But this doesn't work if $VARIABLE has zero length.
Any help will be well appreciated.
Thanks in advance.
Giovanni
# VAR="adfadf"; LENGTH=`expr "$VAR" : '.*'`; echo $LENGTH
I doubt whether your syntax is correct, just use the above it will work correctly
If you want to get 0 for null string put VAR=""
Cheers
You can try this way also if you want as a single command to be executed one at a time
# export VAR="adafadfadf"
# export LENGTH=`expr "$VAR" : '.'`
# echo $LENGTH
10
# export VAR=""
# export LENGTH=`expr "$VAR" : '.'`
# echo $LENGTH
0
#
Regards
Also see the syntax
LENGTH=${#VARIABLE}
You could also use awk:
var="adfadf"
echo $var | awk '{print length($0)}