Commands run on terminal but throw errors in shell script
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.