expr command help

I'm trying to check if a variable'd string is only one character and use that in an if statement the only way I could find is:
$expr "${var}" : . # expr STRING : regrep
where the "." is the grep wildcard for any single character.

Whats wrong with my code here and is there a better way to check if the input is 1 character?

also if i want to pass a variable into a arguement of another function like expr but the content of the variable might cause a syntax error if treat as other then a string, how do i escape ("\") the contents of the variable and not the variable call itself.

Hi.

You could say:

if [ "${#var}" -eq 1 ]; then
  echo only one character long
else
  ...
fi

I've never found any cause to use "expr". Any modern shell has better ways of handling whatever it was good for.

Thank you.

---------- Post updated at 06:32 PM ---------- Previous update was at 06:21 PM ----------

also if i want to pass a variable into a arguement of another function like expr but the content of the variable might cause a syntax error if treat as other then a string, how do i escape ("\") the contents of the variable and not the variable call itself.

There's usually no compelling reason to use expr at all.

The man page says:

I guess that part is left up to the imagination.

If that means using shell, eval, or sed, or whatever, then, whichever way you like.

$ X="\"3\""
$ echo $X
"3"

$ expr $X + 1
expr: non-numeric argument

$ eval expr $X + 1
4

$ Y=${X//\"}
$ echo $Y
3

$ expr $Y + 1
4

$ expr ${X//\"} + 1
4

etc.