I need to write a bash script, that will be executed from an Android device. Among other things, I need this script to count occurrences of a particular character in a string, since wc
(word count) utility is not available in Android shell, I am doing it like this:
my_string="oneX two threeX"; x_amount="${my_string//[^X]}"; echo $x_amount; echo "${#x_amount}"
When I run the above command on desktop, it returns (just as I expect):
XX
2
But if I execute the same command on my Android device (via adb shell
), the result, to my amazement, is:
one two three
13
I have figured out (just by guess) that if I substitute !
for ^
, so that command becomes:
my_string="oneX two threeX"; x_amount="${my_string//[!X]}"; echo $x_amount; echo "${#x_amount}";
then, on Android, it produces the result I expect:
XX
2
While the same command, on desktop, fails with the following message:
event not found: X]
Even thouth I have figured out how to "make it work" I would like to understand the following points:
Where else, besides Android shell,
[!X]
notation is used, instead of[^X]
?Does such notation have any special name?
Are there any specific reason
[^X]
is not supported on Android?
P.S.: the device I need to run script on has a pretty old version of Android (4.4), so this 'issue' might be Android version specific, even if this is the case, questions above remain.