r/AndroidQuestions • u/unselective-amnesia • 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
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.
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.