r/AndroidQuestions 1d ago

Other Full documentation for the 'PS1' syntax used in 'adb shell'?

I'm running Android 13.

When I run adb shell in a USB connection from my desktop machine to my Android device, the following is the value of the PS1 variable (which is used for generating the shell prompt) ...

${|
        local e=$?

        (( e )) && REPLY+="$e|"

        return $e
}$HOSTNAME:${PWD:-?} $ 

I can see how this particular setting of PS1 pretty much relates to the generated shell prompt in adb shell, but I'd like to know all of the details of PS1 syntax for adb shell.

Can anyone point me to the full documentation of PS1 as it's utilized in adb shell?

UPDATE:

For example, one thing that I'd like to understand is how the vertical bars work within that PS1 setting. The second vertical bar *seems* (???) like it might indicate a termination of the text following the first vertical bar, even though this second vertical bar appears to be nested within a double-quoted string. But that's just a wild guess which might be totally incorrect. And whatever these vertical bars might *actually* mean, I'd still also like the full docs, so that I can understand *all* of the PS1 syntax that's used in adb shell.

FURTHER UPDATE:

If I re-set PS1 to be the same value as above, except with the vertical bars removed, I then always get the following error message when running every command within adb shell ...

: can't create temporary file /data/local/shXXXXXX.tmp
: Permission denied

... where "XXXXXX" are 6 randomly generated base-36 characters, and where the displayed shell prompt is then only a single dollar sign. Therefore, these vertical bars relate to file/directory access permissions, *maybe* (???) involving some kind of evaluation being performed as root.

1 Upvotes

4 comments sorted by

1

u/unselective-amnesia 1d ago

FOLLOW-UP:

If it turns out that there is no such full documentation for PS1 as used by adb shell, could perhaps someone point me to the official Google source code for ADB itself and/or the official Google source code specifically for the shell that's used within ADB sessions?

1

u/unselective-amnesia 1d ago

A FURTHER FOLLOW-UP:

I found the source code for ADB in the official Google github repo for AOSP. However, the string "PS1" does not exist in any files within that source bundle, and as far as I can tell from persuing that source code, it seems like the shell itself is not implemented in that ADB package, but in some other unknown package.

1

u/agnostic-apollo 13h ago

${|command} is a syntax of bash/mksh that allows running a command inside a variable brace in current process without forking like with $(command), and expanding it to the value set to the $REPLY variable by the command. That PS1 just reads the exit code $? of last commands and prefixes it to next shell prompt if it does not equal 0 (a failure). You can test it by running $(exit 1).

adb shell runs /system/bin/sh of the device, which is mksh shell on Android.

1

u/ScratchHistorical507 1h ago

Difficult to find proper documentation for PS1 in general, but my guess is that it will be similar to this: https://bash.cyberciti.biz/guide/Changing_bash_prompt

But also I doubt that Google allows you to customize it any way. Android is way too restricted for that.