Commands run on terminal but throw errors in shell script I created a .sh script which sets app permissions on Android devices.
If I push the script on /sdcard and run it through adb shell with command sh ./test.sh, the following error occurs on each permission:

Bad argument: java.lang.IllegalArgumentException: Unknown permission: android.permission.WRITE_EXTERNAL_STORAGE


Here is my Script:

#!/bin/bash
pm grant com.android.camera2 android.permission.RECORD_AUDIO
pm grant com.android.camera2 android.permission.CAMERA
pm grant com.android.camera2 android.permission.ACCESS_FINE_LOCATION
pm grant com.android.camera2 android.permission.WRITE_EXTERNAL_STORAGE
pm grant com.android.camera2 android.permission.READ_EXTERNAL_STORAGE

pm grant com.android.chrome android.permission.CAMERA
pm grant com.android.chrome android.permission.ACCESS_FINE_LOCATION
pm grant com.android.chrome android.permission.WRITE_EXTERNAL_STORAGE
pm grant com.android.chrome android.permission.READ_EXTERNAL_STORAGE
pm grant com.android.chrome android.permission.RECORD_AUDIO


So every time the same command for different permissions and different apps. If I'm using these commands manually on adb shell it works.

I don't have root permissions and the device OS is Android 8.1.0
Apparently there seems no reason for this, except the possible problem with line endings which often happens. On *NIX operating systems (including Android), lines end with Line Feed (LF; \n or 0x0A) while on DOS/Windows with Carriage Return + Line Feed (CRLF; \r\n or 0x0D0x0A). On Mac, only CR is the end-of-line marker. So some text editors may encode wrong line breaks in shell scripts:

~$ cat -v /sdcard/test.sh
#!/bin/bash^M
^M
pm grant com.termux android.permission.WRITE_EXTERNAL_STORAGE^M


Here ^M is the visual representation of CR, which can be fixed with dos2unix, sed or tr utility:

~$ dos2unix /sdcard/test.sh
~$ cat -v /sdcard/test.sh
#!/bin/bash

pm grant com.termux android.permission.WRITE_EXTERNAL_STORAGE


Now the problem should be resolved.