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:

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
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

pm grant com.termux android.permission.WRITE_EXTERNAL_STORAGE

Now the problem should be resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.