Question: How should I get `$?` of `adb shell <command>`?

I would like to check the result of `adb shell mkdir /xxx`.

I executed `mkdir` command by `adb shell` and failed but the result of `$?` is `0`.

```
$ adb shell mkdir /xxx
mkdir failed for /xxx, Read-only file system
$ adb shell echo $?
0

$ adb shell "mkdir /xxx; echo $?"
mkdir failed for /xxx, Read-only file system
0

```

I would like to get the result code of `adb shell <command>` **but not in the interactive mode** like below:

```
$ adb shell
shell@android:/ $ mkdir /xxx
mkdir failed for /xxx, Read-only file system
255|shell@android:/ $ echo $?
255

```

Answer: 
You should do:

```
$ adb shell 'mkdir /xxx; echo $?'
mkdir failed for /xxx, Read-only file system
255

```

**Notice the single quotes**, otherwise `$?` is evaluated before reaching adb.
If you are using an older emulator, then `adb shell` does not return the exit value. Then you have to use `adb shell 'false; echo $?'` like in <https://stackoverflow.com/a/17480194/306864>

In newer emulators, it works properly, then you can do `adb shell false || echo failed`. Here's how I tested:

```sh
$ adb -e android-22 shell false; echo $?
0
$ adb -e android-22 shell 'false; echo $?'
1
$ adb -e android-29 shell false; echo $?
1
$ adb -e android-29 shell 'false; echo $?'
1

```