6

Hi I need to run things in the form of adb shell <command>

When I test everything out inside adb shell, it works because I was able to set some aliases in .bashrc. However, when I do adb shell <command>, nothing works because .bashrc is not used when you run adb shell <command>, because it's in the non-interactive mode.

How can I work around this? Can I adb push some files to the filesystem so that the alias will be there when adb shell is run?

Alex P.
  • 30,437
  • 17
  • 118
  • 169
m126531
  • 265
  • 1
  • 3
  • 11

3 Answers3

16

If your android device is rooted you can add your aliases for adb shell into the /system/etc/mkshrc file.

Alex P.
  • 30,437
  • 17
  • 118
  • 169
  • 1
    If you get a "Read-only file system" error, boot into TWRP or whatever custom recovery, mount system, then edit the file. – Paul J Dec 29 '15 at 22:09
  • 4
    A got that read-only error, but booting into TWRP didn't work for me, because /system didn't have the `etc/mkshrc` file as when booted normally. I used `mount -o remount,rw /system` without booting into TWRP to mount `/system` read-write. – Johannes Bittner Feb 20 '17 at 16:44
  • 2
    @JohannesBittner ... to complement your excellent comment: To remount read-only again (for security reasons) use this command in a terminal as root (su): `mount -o remount,ro /system` – R Yoda Jan 06 '18 at 13:42
  • @R Yoda. Nowadays since android 9, use `/` instead of `/system`. `mount -o,rw $(mount | grep /dev/root | awk '{print $3}'` does the job for any android version. – 5p0ng3b0b Feb 13 '20 at 15:04
  • oops missed closing bracket etc. `mount -o rw,remount $(mount | grep /dev/root | awk '{print $3}')` – 5p0ng3b0b Feb 13 '20 at 15:11
  • Adding alias to `/system/etc/mkshrc` only works after getting the `adb shell` prompt. It does *not* work if the alias is invoked using `adb shell ""`. (I have a rooted Android-10 based device). – Tzunghsing David Wong Jan 21 '23 at 16:46
0

One way to do this is to issue several shell commands in a single ADB command. You can put them in a string, and separate them with semicolons, thus:

adb shell "alias foo bar ; export MY_VARIABLE=/path/to/somewhere ; my_executable "

The " are crucial here, make sure they are paired up correctly. You could run your .bashrc this way, thus:

adb shell "source /path/to/.bashrc ; my_executable"

John Dallman
  • 590
  • 4
  • 18
-3

You can write a bash script that sets the aliases and then executes your shell:

#!/usr/bin/bash
. $HOME/.bashrc
adb shell $@
Bruce Barnett
  • 904
  • 5
  • 11