Is it possible to execute Shell scripts from Android application

So far as I have tried, it works. I think it is supposed to, because that is how many Linux GUI apps do some of their work, by issuing shell commands.

To make sure, I tried issuing a vanilla command's output over to Logcat on an old, low-end, unrooted Android 6.0 phone, and it worked (working code below).

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] cmd = new String[]{"ls", "-la", "/"};

        try {
            // These two lines are what we care about
            Process process = Runtime.getRuntime().exec(cmd);
            InputStream iStream = process.getInputStream();

            // This is how we check whether it works
            tryWriteProcessOutput(iStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void tryWriteProcessOutput(InputStream iStream) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));

            String output = "";
            String line;

            try {
                while ((line = reader.readLine()) != null) {
                    output += line + "\n";
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                reader.close();
            }

        Log.d("cmdOutput", output);
    }
}

However, your mileage might vary wildly here. With so many Android manufacturers, I would expect different versions of the command shell on different devices, and thus I wouldn't expect every Android device to be able to run just any command I threw at it, unless it's a really common one.

Besides, you might also run into problems with system permissions with the commands themselves rather than the command shell (ie. busybox: Permission denied).


Termux is a terminal emulator but doesn't have access to everything in the Android OS. Eventually its an app and will face restrictions as any other app does in the Android. APT, pkg and other package installers or packages are installed within the emulator but not as system wide. Also certain commands will have restrictions similar to your case for example pm can only be executed with root or an adb shell since their user IDs are allowed to perform such actions while other apps are not. Meaning running the command pm install -r -t someapp.apk will give an error in Termux while it would certainly work if using adb shell or having root permissions.

To make things even more clear, running the command echo $PATH in Termux will show directories whose Termux commands are only included, and they are not Android OS commands but compiled packages that can run on Android OS. It is a nice way of like running things on top Android without having to deal with a lot of restrictions, more like a sand-boxed environment. Installing certain packages that need to go out of the sandbox will face issues so be mindful of that.