Search code examples
androidshelladbexit-code

Get result code of adb shell command


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

Solution

  • 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.