Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A bash script that let's you issue adb commands to multiple devices at once
#!/bin/bash
# Script adb+
# Usage
# You can run any command adb provides on all your currently connected devices
# ./adb+ <command> is the equivalent of ./adb -s <serial number> <command>
#
# Examples
# ./adb+ version
# ./adb+ install apidemo.apk
# ./adb+ uninstall com.example.android.apis
adb devices | while read line
do
if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
then
device=`echo $line | awk '{print $1}'`
echo "$device $@ ..."
adb -s $device $@
fi
done
@toddlikesbeer

This comment has been minimized.

Show comment Hide comment
@toddlikesbeer

toddlikesbeer Dec 2, 2014

Just wanted to let you know I'm using this, works great; Thanks.

Just wanted to let you know I'm using this, works great; Thanks.

@thebagchi

This comment has been minimized.

Show comment Hide comment
@thebagchi

thebagchi Jan 18, 2016

Hi, I have created the windows version of the same at ADB+.BAT!

Hi, I have created the windows version of the same at ADB+.BAT!

@luisvasconcelos

This comment has been minimized.

Show comment Hide comment
@luisvasconcelos

luisvasconcelos May 9, 2016

Still using it thanks Mr. Chris P.

Still using it thanks Mr. Chris P.

@Dgilliams

This comment has been minimized.

Show comment Hide comment
@Dgilliams

Dgilliams Aug 10, 2016

This has stopped working for me. For some reason my terminal is only reading the first line. Below is my adjusted code to debug and also my output.

It still works fine with one device. If multiple are connected it will only send the command to the first device listed.

filename = adb+

command issued: adb+ shell input text win
expected outcome: the string "win" typed on to every device connected.

//~~~~~~~~~ Code ~~~~~~~~~~

#!/bin/bash
# "pipe" the listed devices to a while loop

echo print out devices
adb devices
adb devices | while read line
do
  echo start of whileloop
    # if it's not empty, use the next line
    if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ];
    then
      echo test, start of ifstatement
      echo $line
      echo end test

        # assign device variable to the result of printing the first token
        device=`echo $line | awk '{print $1}'`

        # simple print out, letting user know something is happening
        echo "$device $@ ..."

        # run the adb commands you set as an argument to "adb+"
        adb -s $device $@
        echo endofif
    fi
    echo end of whileloop
done

//~~~~~~~~ Output~~~~~~~~~

$ adb+ shell input text win
print out devices
List of devices attached
13564a0d    device
84B7N16315001956    device

start of whileloop
end of whileloop
start of whileloop
test, start of ifstatement
13564a0d device
end test
13564a0d shell input text win ...
84B7N16315001956    device

endofif
end of whileloop

Dgilliams commented Aug 10, 2016

This has stopped working for me. For some reason my terminal is only reading the first line. Below is my adjusted code to debug and also my output.

It still works fine with one device. If multiple are connected it will only send the command to the first device listed.

filename = adb+

command issued: adb+ shell input text win
expected outcome: the string "win" typed on to every device connected.

//~~~~~~~~~ Code ~~~~~~~~~~

#!/bin/bash
# "pipe" the listed devices to a while loop

echo print out devices
adb devices
adb devices | while read line
do
  echo start of whileloop
    # if it's not empty, use the next line
    if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ];
    then
      echo test, start of ifstatement
      echo $line
      echo end test

        # assign device variable to the result of printing the first token
        device=`echo $line | awk '{print $1}'`

        # simple print out, letting user know something is happening
        echo "$device $@ ..."

        # run the adb commands you set as an argument to "adb+"
        adb -s $device $@
        echo endofif
    fi
    echo end of whileloop
done

//~~~~~~~~ Output~~~~~~~~~

$ adb+ shell input text win
print out devices
List of devices attached
13564a0d    device
84B7N16315001956    device

start of whileloop
end of whileloop
start of whileloop
test, start of ifstatement
13564a0d device
end test
13564a0d shell input text win ...
84B7N16315001956    device

endofif
end of whileloop
@trebouillon

This comment has been minimized.

Show comment Hide comment
@trebouillon

trebouillon Oct 26, 2016

I had the same issue like above and found some inspiration here: http://stackoverflow.com/questions/17047659/bash-enumerate-all-the-attached-devices

I changed my script to:

for line in `adb devices | grep -v "List"  | awk '{print $1}'`
do
  device=`echo $line | awk '{print $1}'`
  echo "$device $@ ..."
  adb -s $device $@
done

This works for me.

trebouillon commented Oct 26, 2016

I had the same issue like above and found some inspiration here: http://stackoverflow.com/questions/17047659/bash-enumerate-all-the-attached-devices

I changed my script to:

for line in `adb devices | grep -v "List"  | awk '{print $1}'`
do
  device=`echo $line | awk '{print $1}'`
  echo "$device $@ ..."
  adb -s $device $@
done

This works for me.

@sivze

This comment has been minimized.

Show comment Hide comment
@sivze

sivze Apr 13, 2017

No script needed.
This command works perfect
adb devices | awk 'NR>1{print $1}' | xargs -n1 -I% adb -s % install foo.apk

sivze commented Apr 13, 2017

No script needed.
This command works perfect
adb devices | awk 'NR>1{print $1}' | xargs -n1 -I% adb -s % install foo.apk

@wheelsmanx

This comment has been minimized.

Show comment Hide comment
@wheelsmanx

wheelsmanx Apr 2, 2018

thebagchi
Thank you for sharing your .bat I look forward to testing it.

thebagchi
Thank you for sharing your .bat I look forward to testing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment