16

Does anyone know how to run commands from adb shell and remain in shell session? What I`m trying to achieve is to set aliases in adb shell.

I have tried the following without success

adb shell <<< "ls"

After executing this command indeed remain in shell, but cannot receive output of any further command.

I have also tried the following:

adb shell <<EOF
ls
EOF

with the same result.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
sashas
  • 531
  • 1
  • 3
  • 14

4 Answers4

30

When you run:

adb shell ls

You are running this command currently outside of ADB.

First, you need to enter ADB:

adb shell

Once you enter ADB shell, you can continue to see output and input more commands.

ls
help

To exit ADB, simply type "exit" or hit "Ctrl + C"

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
8

expect solution

This will run the command, and leave you in an ADB shell automatically.

adb-cmd

#!/usr/bin/env expect
spawn adb shell
expect "#"
send [ concat [ join $argv " " ] ]
send "\r"
interact

Usage:

adb-cmd 'cd /data/data; ls'

Tested in Ubuntu 16.04 host, Android O guest.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
5

There was a similar question answered in the comments here.

In short, run the following from your terminal:

stty raw -echo ; ( echo "ls" && cat ) | adb shell ; stty sane

Note: without the stty magic, the command is piped to adb and tab complete etc. is not recognized.

Community
  • 1
  • 1
0
            //you can use a nodejs script as below:
            //let input='shell script here' 
            let input='cd /data/local/tmp\nchmod +x a.out\n./a.out\n'
            p=run("adb.exe",args:'shell',cb:rs=>lg(rs)})
            p.stdout.on("data",rs=>{
            lg(rs)
            setInterval(_=>process.exit(),2000);
            })
            p.stdin.write(input)

            //+++++++++++++++++++++++++
            function lg(...args){
            console.log(...args);
            }

            function run(cmd,args,cb){
            if(args) cmd= cmd+" "+args;
            return require('child_process').exec(cmd,(er,stdout,stderr)=>{ 
            if(er) return cb(stdout||stderr,false);
            stdout=stdout==''?true:stdout;
            cb(stdout,true)

            });
            }
  • [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – Muhammad Mohsin Khan Mar 01 '22 at 14:20