4

I am trying to find differences between Linux scripts and Android scripts. But I cannot find any specific differences in syntax. For example things that the Android scripts can do but Linux scripts can't, execution, performance, etc.

Can you suggest something specific to look for?

user418889
  • 103
  • 4
Kreator
  • 57

2 Answers2

10

Android runs MirBSD Korn shell mksh by default which is mostly a superset of sh. That has some limitations compared to bash scripts, for example one can't use arrays.

Furthermore core utils are android specific toolbox or toybox applets, usage often is slightly different and sometimes limited (for example tar)

Customized android devices use busybox as extension for missing core utils, which applets also depend on the build.

Android permissions are extended with SELinux which requires different way of scripting when it comes to file manipulation, for example one can't just use sed as usual because the file will lose it's secontext.

one should always test scripts against as much shells as possible (mksh, ash) and keep usage of core utils simple (for example code should work with BSD grep, toybox grep and busybox grep)

Avoid interactive scripts as the device has no real keyboard and the tiny touchscreen is a hassle when it comes to command line confirmation.

Dealing with paths is also slightly different on android devices, for example shebang is #!/system/bin/sh while /data/local/tmp directory is persistent on user space (not in RAM)


Worth mention is adb shell it has more privileges as any other terminal emulator by default. scripts running on host using adb commands should be clearly provided as such, inexperienced users may confuse them with shell scripts running on client.


When it comes to scripts running in recovery mode there is one related script language EDIFY and it's counterpart shell script replacement. the shell in recovery mode (ash) is different from android boot mode (mksh) and behavior depends on recovery build. one should bundle busybox and all necessary binaries to avoid trouble with missing applets (like awk)


The script language itself is not different and there is no specific syntax. Porting bash scripts to android is absolutely possible and requires no special knowledge. It's all just about different environment, therefore scripts should be tested on target platform

That said, Happy scripting :)

alecxs
  • 564
5

There isn't a single shell for Android or Linux and some defaults have changed over the years, so you might have different default shells in different devices and Linux/Unix and Android distribution (say Lineage OS, Debian, AIX).

Many of those systems include more than one shell, meaning that you could run scripts with ash, bash, ksh, ... by changing the shebang or log into them with something like adb shell -t bash -i, so, in the end, the comparison is between different shells.

That's why it would be very hard to list the differences in syntax or other issues based on Android or Linux.