8

When I do enter some text with & character then my complete text is not entered on the device.

D:\Apps\Android-SDK\tools>adb shell input text hello&hello
'hello' is not recognized as an internal or external command,
operable program or batch file.

It entered only hello. but the other & and hello characters are not entered.

How can I enter the & character?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
a.t.obulreddy
  • 239
  • 2
  • 4
  • 13
  • Duplicate of [How to input ampersand with adb shell input?](http://stackoverflow.com/questions/28468279/how-to-input-ampersand-with-adb-shell-input) – Dan Dascalescu Dec 11 '15 at 02:07

4 Answers4

11

You need to encapsulate your string in quotes and escape your ampersand "hello\&hello"

Stephen
  • 4,041
  • 22
  • 39
  • No its not entering. Please find the below output. D:\Apps\Android-SDK\tools>adb shell input text hello1\&hello2 'hello2' is not recognized as an internal or external command, operable program or batch file. – a.t.obulreddy Sep 11 '14 at 15:34
  • Nope still the same error. D:\Apps\Android-SDK\tools>adb shell input text "hello12&hello12" /system/bin/sh: hello12: not found – a.t.obulreddy Sep 11 '14 at 15:35
5

( ) < > | ; & * \ ~ " ' and space all need escaping. Space can be replaced with %s.

I have written an android command line tool to do this call inputer.
A function from my code is here (uses regex):

//e.g. convert string "hello world\n" to string "hello%sworld\n"
    char * convert(char * trans_string)
    {
        char *s0 = replace(trans_string, '\\', "\\\\");
        free(trans_string);
        char *s = replace(s0, '%', "\\\%");
        free(s0);
        char *s1 = replace(s, ' ', "%s");//special case for SPACE
        free(s);
        char *s2 = replace(s1, '\"', "\\\"");
        free(s1);
        char *s3 = replace(s2, '\'', "\\\'");
        free(s2);
        char *s4 = replace(s3, '\(', "\\\(");
        free(s3);
        char *s5 = replace(s4, '\)', "\\\)");
        free(s4);
        char *s6 = replace(s5, '\&', "\\\&");
        free(s5);
        char *s7 = replace(s6, '\<', "\\\<");
        free(s6);
        char *s8 = replace(s7, '\>', "\\\>");
        free(s7);
        char *s9 = replace(s8, '\;', "\\\;");
        free(s8);
        char *s10 = replace(s9, '\*', "\\\*");
        free(s9);
        char *s11 = replace(s10, '\|', "\\\|");
        free(s10);
        char *s12 = replace(s11, '\~', "\\\~");
        //this if un-escaped gives current directory !
        free(s11);
        char *s13 = replace(s12, '\¬', "\\\¬");
        free(s12);
        char *s14 = replace(s13, '\`', "\\\`");
        free(s13);
    //  char *s15 = replace(s14, '\¦', "\\\¦");
    //  free(s14);
        return s14;
}
Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54
0

I wrote a simple oneliner to escape the input text:

"Hello, world.".replace(/[()<>|;&*\\~"'$]/g, function (match) { return ("\\" + match); }).replace(/ /g, "%s");
//

You may have to adjust it or add more escape characters.

XP1
  • 6,910
  • 8
  • 54
  • 61
0

Finally found a string that works for me - hello1"&"hello2

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30182539) – Saeid Lotfi Oct 27 '21 at 08:19