31

I need to make a script that executes a lots of thing on Android device, my device is rooted, when I enter on the shell, I can give the command su, and it works but I need pass this command like:

adb shell "
su;
mv /sdcard/Download/app_test /data/local;
cd /data/local;
./app_test;
exit;
exit;
"

when I put some commands before the su it works, according what I read su creates a new shell that return immediately, but I need give commands on the su shell, how can I do that?

Alex
  • 3,301
  • 4
  • 29
  • 43
  • 2
    `su -c` will invoke a command based on su's arguments. – Marc B Dec 03 '14 at 14:38
  • If you need to have root access on emulators - check https://stackoverflow.com/questions/43923996/adb-root-is-not-working-on-emulator-cannot-run-as-root-in-production-builds/45668555#45668555. – Robert Lujo Sep 03 '19 at 08:55

7 Answers7

33

Well, if your phone is rooted you can run commands with the su -c command.

Here is an example of a cat command on the build.prop file to get a phone's product information.

adb shell "su -c 'cat /system/build.prop |grep "product"'"

This invokes root permission and runs the command inside the ' '.

Notice the 5 end quotes, that is required that you close ALL your end quotes or you will get an error.

For clarification the format is like this.

adb shell "su -c '[your command goes here]'"

Make sure you enter the command EXACTLY the way that you normally would when running it in shell.

Edric
  • 24,639
  • 13
  • 81
  • 91
A-Droid Tech
  • 2,301
  • 24
  • 33
  • 3
    For me your solution didn't work instead `adb shell "su 0 command"` works and here is one example `adb shell "su 0 chmod 644 /system/priv-app/myApp.apk"` – Alireza Ahmadi Jul 30 '18 at 07:19
  • What if the command I need to put already had `"` ? For example I need to run this command to execute a scheduled backup of SwittBackup app (https://swiftapps.org/faq#schedulecommand) : `am start -n org.swiftapps.swiftbackup/.shortcuts.ShortcutsActivity -e "cmd" "-s vmppnc" ` . How can I pass it correctly to `adb shell "su ` ? – android developer Dec 01 '22 at 18:07
12

On my Linux, I see an error with

adb shell "su -c '[your command goes here]'"

su: invalid uid/gid '-c'

The solution is on Linux

adb shell su 0 '[your command goes here]'
hannes ach
  • 16,247
  • 7
  • 61
  • 84
7

The su command does not execute anything, it just raise your privileges.

Try adb shell su -c YOUR_COMMAND.

shkschneider
  • 17,833
  • 13
  • 59
  • 112
3

1. adb shell su

win cmd

C:\>adb shell id
uid=2000(shell) gid=2000(shell)

C:\>adb shell 'su -c id'
/system/bin/sh: su -c id: inaccessible or not found

C:\>adb shell "su -c id"
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0

C:\>adb shell su -c id   
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0

win msys bash

msys2@bash:~$ adb shell 'su -c id'
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
msys2@bash:~$ adb shell "su -c id"
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
msys2@bash:~$ adb shell su -c id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0

2. adb shell -t

if want run am cmd, -t option maybe required:

C:\>adb shell su -c am stack list
cmd: Failure calling service activity: Failed transaction (2147483646)

C:\>adb shell -t su -c am stack list
Stack id=0 bounds=[0,0][1200,1920] displayId=0 userId=0
...

shell options:

 shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]
     run remote shell command (interactive shell if no command given)
     -e: choose escape character, or "none"; default '~'
     -n: don't read from stdin
     -T: disable pty allocation
     -t: allocate a pty if on a tty (-tt: force pty allocation)
     -x: disable remote exit codes and stdout/stderr separation

Android Debug Bridge version 1.0.41
Version 30.0.5-6877874

yurenchen
  • 1,897
  • 19
  • 17
2

By default CM10 only allows root access from Apps not ADB. Go to Settings -> Developer options -> Root access, and change option to "Apps and ADB".

1

for my use case, i wanted to grab the SHA1 hash from the magisk config file. the below worked for me.

adb shell "su -c "cat /sbin/.magisk/config | grep SHA | awk -F= '{ print $2 }'""
shr00mie
  • 31
  • 3
1

I opt for the following:

adb shell su root <your command>

e.g., to access the external storage (sd card):

adb shell su root ls storage/0/emulated
auermich
  • 130
  • 10