The adb shell was a direct descendant of BSD ash, of which dash is also.
The adb shell in more recent android versions is mksh standard shell of non-emulator builds on Android 4.0 and newer.
From the error text it is probable that the shell is some mksh version.
As the mksh in android is under heavy changes and improvements, I recommend you to take dash as the target shell. It is available in most linux distributions and is the default system shell for debian and derivatives. It has less features but it is very difficult that something that work in dash will not work in the mksh of adb.
{1:3:1}
In particular, this expression:
x=${1:3:1}
has no meaning in dash (it does work, however, in mksh). It could be replaced with this:
x=${1#???}; x=${x%"${x#?}"}
A bit longer, but gets the job done in any shell.
[
Also, there is a problem with the […]
test, it seems to have too many "parts" to be reliable. Dividing the test into two (or more if needed) is the usual remedy:
if [ "${x}" = " " ] || [ -z "${x}" ]; then
The ||
is also an or (with the same preference as &&
, the and).
Which also works in any shell.
With a final change of (a missing $ for variable x):
echo "A - ${a} X - ${x}"
We get the whole script as:
func()
{
local x
x=${1#???}; x=${x%${x#?}}
echo "x - $x"
if [ "${x}" = " " ] || [ -z "${x}" ]; then
a="M"
else
a="S"
fi
echo "A - ${a} X - ${x}"
}
func "abc=efg"
And running it in dash, bash or mksh, gives:
x - =
A - S X - =