15

How to send text with spaces like "some text" using adb shell input text ?

Found following solution

adb shell input text "some%stext" is working fine. But any easy way to replace space with %s?

Example:

$ adb shell input text "some text"
Error: Invalid arguments for command: text
Usage: input [<source>] <command> [<arg>...]

The sources are: 
      keyboard
      mouse
      joystick
      touchnavigation
      touchpad
      trackball
      dpad
      stylus
      gamepad
      touchscreen

The commands and default sources are:
      text <string> (Default: touchscreen)
      keyevent [--longpress] <key code number or name> ... (Default: keyboard)
      tap <x> <y> (Default: touchscreen)
      swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
      press (Default: trackball)
      roll <dx> <dy> (Default: trackball)
Lava Sangeetham
  • 2,943
  • 4
  • 38
  • 54

6 Answers6

23

You can do it with \<space>, like this:

adb shell input text "some\ text"
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
KS Ravikumar
  • 254
  • 2
  • 2
8

With gnu-linux sed (or others) installed (most linux machines come with it preinstalled) - you could use sed to replace spaces with %s.

adb shell input text $(echo "some text with spaces" | sed 's/ /\%s/g')

the %-sign has to be escaped with \.

cde
  • 317
  • 1
  • 18
kai-dj
  • 315
  • 1
  • 18
  • Sed is also available in most busy box implementations found on android or added after rooting. As $() is interpreted locally since you are using `adb shell input`, instead of inside of the android system (aka `adb shell`) you are using your system's sed. The stripped down minimal toolbox does not have sed. – cde Mar 15 '18 at 23:38
  • the Q asks to use adb shell input, not for text manipulation inside adb shell ^^ – kai-dj Mar 16 '18 at 00:10
  • What encoding type is this ? What about ':' can be useful to pass URLs ? – RzR Mar 17 '19 at 18:20
2

You can send your text like:

Text = "some text with spaces"

Replace blanks with %s

adb shell input text some%stext%swith%sspaces
Ingo
  • 5,239
  • 1
  • 30
  • 24
  • This does not actually answer op's question. Op knows you need %s, they did not want to manually edit all the spaces. – cde Mar 15 '18 at 23:40
  • @cde I of course assumed that a string replace function would be used to change blanks to %s – Ingo Feb 23 '21 at 00:36
1

adb shell "input text 'some text'"

Yong
  • 1,529
  • 12
  • 21
0

The best way replaces special characters with .

 val message = text.replace("|", "\\|")
            .replace("\"", "\\\"")
            .replace("\'", "\\\'")
            .replace("<", "\\<")
            .replace(">", "\\>")
            .replace(";", "\\;")
            .replace("?", "\\?")
            .replace("`", "\\`")
            .replace("&", "\\&")
            .replace("*", "\\*")
            .replace("(", "\\(")
            .replace(")", "\\)")
            .replace("~", "\\~")
            .replace(" ", "\\ ")
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
0

Enclose the text in single quote:

adb shell input text 'some text'
SuB
  • 2,250
  • 3
  • 22
  • 37