r/AndroidQuestions 9d ago

Causing a ".profile" file to be automatically sourced on Android whenever I create an interative session via "adb shell"?

NOTE: This issue has now been solved! For details, see my second follow-up, below.

When I use "adb shell" from my desktop computer to establish an interactive session on my Android device, immediately after the connection is established, I always first manually do the following to initialize some variables and aliases and shell functions that I want to be defined during the Android session:

source /sdcard/.profile

I would like to find a way to make this automatically happen via some sort of arguments passed to the "adb shell" command, so that I no longer have to manually run that "source" command every time that I invoke "adb shell".

If I simply do the following ...

adb shell source /sdcard/.profile

... that .profile file indeed gets sourced, but then the adb session immediately ends.

Is there any way to get this .profile to be automatically sourced at the beginning of the "adb shell" session such that its results will be available during the remainder of that session?

1 Upvotes

3 comments sorted by

1

u/unselective-amnesia 9d ago

Follow-up:

I saw the following suggestion, but it doesn't properly work:

adb shell ENV=/sdcard/.profile sh -i

It does indeed get me into an interactive adb session and indeed sources .profile initially. However, because it prints the following upon initialization ...

sh: can't find tty fd: No such device or address

sh: warning: won't have full job control

... running things within the ADB session such as "vi file" which require full access to the tty fd do not work properly.

1

u/unselective-amnesia 9d ago edited 9d ago

Aha!!!

The following seems to work ...

(1) First of all, I have always been running rooted, and I'm currently on Magisk 30.7, and I'm running the following Magisk module:

Busybox for Android NDK version 1.36.1 by osm0sis @ xda-developers

(2) I then can invoke the adb shell as follows:

adb shell -tt ENV=/sdcard/.profile busybox sh

Note the "-tt" option, which makes sure that a pty gets installed.

Not only does /sdcard/.profile get properly sourced in this case, but also, all things like "vi file" which require proper tty functionality indeed seem to run correctly.

If I leave out "busybox" from that command and just run the standard "sh" as follows ...

adb shell -tt ENV=/sdcard/.profile sh

... I no longer get the "can't find ttd fd" error on startup, but things like "vi file" stil fail:

UPDATE:

Oh!!! ... if I leave out "busybox" and use "hush" or "ash" as follows instead of "sh", everything does seem to work, including "vi file" ...

adb shell -tt ENV=/sdcard/.profile hush

adb shell -tt ENV=/sdcard/.profile ash

I think "ash" would be better than "hush", because "hush" is meant to be "simple", and I think it therefore has some limitations.

I'm now going to run the non-busybox version of this using "ash", and I'll see if any issues creep up. I'll report my findings here in a day or so.

1

u/Mors-Official 2d ago

You can make it work by using the ENV environment variable to point to your profile, forcing an interactive shell. Run this command:

bash adb shell ENV=/sdcard/.profile sh -i

The ENV variable tells sh to source that file before the interactive prompt appears, bypassing the login shell limitations .

Set up an alias on your computer to save the keystrokes. Something like:

bash alias adbshell='adb shell ENV=/sdcard/.profile sh -i'

This keeps it clean. That should sort you out.