input text escape #57

Open
opened 2026-01-18 00:53:33 +01:00 by Benjamin_Loison · 7 comments

~ source: issues/51#issuecomment-4431 does not help.

DuckDuckGo search adb escape write text.

The Stack Overflow question 25791423 raises this concern, but answers may not be exhaustive.

Would help Benjamin_Loison/Backup_Android_apps/issues/50.

`~` source: [issues/51#issuecomment-4431](https://gitea.lemnoslife.com/Benjamin_Loison/adb/issues/51#issuecomment-4431) does not help. DuckDuckGo search *adb escape write text*. [The Stack Overflow question 25791423](https://stackoverflow.com/q/25791423) raises this concern, but answers may not be exhaustive. Would help [Benjamin_Loison/Backup_Android_apps/issues/50](https://codeberg.org/Benjamin_Loison/Backup_Android_apps/issues/50).
Author
Owner
    for character in '(>':
        text = text.replace(character, rf'\{character}')
shlex.quote(...)

seems to work fine.

```python for character in '(>': text = text.replace(character, rf'\{character}') ``` ```python shlex.quote(...) ``` seems to work fine.
Author
Owner

With ( in clipboard:

Python script:
import subprocess
import pyperclip

clipboard = pyperclip.paste()
subprocess.check_output(f'adb shell input text {clipboard}', shell = True)
Output:
Exception occurred while executing 'text':
java.lang.IllegalArgumentException: Argument expected after "text"
        at com.android.modules.utils.BasicShellCommandHandler.getNextArgRequired(BasicShellCommandHandler.java:295)
        at com.android.server.input.InputShellCommand.runText(InputShellCommand.java:365)
        at com.android.server.input.InputShellCommand.onCommand(InputShellCommand.java:290)
        at com.android.modules.utils.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:97)
        at android.os.ShellCommand.exec(ShellCommand.java:38)
        at com.android.server.input.InputManagerService.onShellCommand(InputManagerService.java:3887)
        at android.os.Binder.shellCommand(Binder.java:1151)
        at android.os.Binder.onTransact(Binder.java:953)
        at android.hardware.input.IInputManager$Stub.onTransact(IInputManager.java:1576)
        at android.os.Binder.execTransactInternal(Binder.java:1426)
        at android.os.Binder.execTransact(Binder.java:1365)
/bin/sh: 2: Syntax error: word unexpected (expecting ")")
Traceback (most recent call last):
  File "<tmp 5>", line 6, in <module>
    subprocess.check_output(f'adb shell input text {clipboard}', shell = True)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/subprocess.py", line 472, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               **kwargs).stdout
               ^^^^^^^^^
  File "/usr/lib/python3.13/subprocess.py", line 577, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'adb shell input text 
subprocess.check_output(f'adb shell input text {shlex.quote(clipboard)}', shell = True)' returned non-zero exit status 2.
With *(* in `clipboard`: <details> <summary>Python script:</summary> ```python import subprocess import pyperclip clipboard = pyperclip.paste() subprocess.check_output(f'adb shell input text {clipboard}', shell = True) ``` </details> <details> <summary>Output:</summary> ``` Exception occurred while executing 'text': java.lang.IllegalArgumentException: Argument expected after "text" at com.android.modules.utils.BasicShellCommandHandler.getNextArgRequired(BasicShellCommandHandler.java:295) at com.android.server.input.InputShellCommand.runText(InputShellCommand.java:365) at com.android.server.input.InputShellCommand.onCommand(InputShellCommand.java:290) at com.android.modules.utils.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:97) at android.os.ShellCommand.exec(ShellCommand.java:38) at com.android.server.input.InputManagerService.onShellCommand(InputManagerService.java:3887) at android.os.Binder.shellCommand(Binder.java:1151) at android.os.Binder.onTransact(Binder.java:953) at android.hardware.input.IInputManager$Stub.onTransact(IInputManager.java:1576) at android.os.Binder.execTransactInternal(Binder.java:1426) at android.os.Binder.execTransact(Binder.java:1365) /bin/sh: 2: Syntax error: word unexpected (expecting ")") Traceback (most recent call last): File "<tmp 5>", line 6, in <module> subprocess.check_output(f'adb shell input text {clipboard}', shell = True) ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.13/subprocess.py", line 472, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ **kwargs).stdout ^^^^^^^^^ File "/usr/lib/python3.13/subprocess.py", line 577, in run raise CalledProcessError(retcode, process.args, output=stdout, stderr=stderr) subprocess.CalledProcessError: Command 'adb shell input text subprocess.check_output(f'adb shell input text {shlex.quote(clipboard)}', shell = True)' returned non-zero exit status 2. ``` </details>
Author
Owner
import shlex

subprocess.check_output(f'adb shell input text {shlex.quote(clipboard)}', shell = True)
Output:
/system/bin/sh: import: inaccessible or not found
Traceback (most recent call last):
  File "<tmp 5>", line 4, in <module>
    subprocess.check_output(f'adb shell input text {shlex.quote(clipboard)}', shell = True)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/subprocess.py", line 472, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               **kwargs).stdout
               ^^^^^^^^^
  File "/usr/lib/python3.13/subprocess.py", line 577, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'adb shell input text 'import subprocess
import pyperclip
'' returned non-zero exit status 127.
```python import shlex subprocess.check_output(f'adb shell input text {shlex.quote(clipboard)}', shell = True) ``` <details> <summary>Output:</summary> ``` /system/bin/sh: import: inaccessible or not found Traceback (most recent call last): File "<tmp 5>", line 4, in <module> subprocess.check_output(f'adb shell input text {shlex.quote(clipboard)}', shell = True) ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.13/subprocess.py", line 472, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ **kwargs).stdout ^^^^^^^^^ File "/usr/lib/python3.13/subprocess.py", line 577, in run raise CalledProcessError(retcode, process.args, output=stdout, stderr=stderr) subprocess.CalledProcessError: Command 'adb shell input text 'import subprocess import pyperclip '' returned non-zero exit status 127. ``` </details>
Author
Owner
subprocess.check_output(['adb', 'shell', 'input', 'text', clipboard])
Output:
/system/bin/sh: import: inaccessible or not found
Traceback (most recent call last):
  File "<tmp 5>", line 4, in <module>
    subprocess.check_output(['adb', 'shell', 'input', 'text', clipboard])
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/subprocess.py", line 472, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               **kwargs).stdout
               ^^^^^^^^^
  File "/usr/lib/python3.13/subprocess.py", line 577, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['adb', 'shell', 'input', 'text', 'import subprocess\nimport pyperclip\n']' returned non-zero exit status 127.
```python subprocess.check_output(['adb', 'shell', 'input', 'text', clipboard]) ``` <details> <summary>Output:</summary> ``` /system/bin/sh: import: inaccessible or not found Traceback (most recent call last): File "<tmp 5>", line 4, in <module> subprocess.check_output(['adb', 'shell', 'input', 'text', clipboard]) ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.13/subprocess.py", line 472, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ **kwargs).stdout ^^^^^^^^^ File "/usr/lib/python3.13/subprocess.py", line 577, in run raise CalledProcessError(retcode, process.args, output=stdout, stderr=stderr) subprocess.CalledProcessError: Command '['adb', 'shell', 'input', 'text', 'import subprocess\nimport pyperclip\n']' returned non-zero exit status 127. ``` </details>
Author
Owner
Related to [Benjamin_Loison/virt-manager/issues/9](https://codeberg.org/Benjamin_Loison/virt-manager/issues/9).
Author
Owner

%s replaces s with a space, while %a works fine for instance...

Would help Benjamin_Loison/Backup_Android_apps/issues/60.

`%s` replaces `s` with a space, while `%a` works fine for instance... Would help [Benjamin_Loison/Backup_Android_apps/issues/60](https://codeberg.org/Benjamin_Loison/Backup_Android_apps/issues/60).
Author
Owner

Writing character by character deserves working and is not that slow.

Writing character by character deserves working and is not that slow.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Benjamin_Loison/adb#57