Bad substitution error in bash script Bad substitution error in bash script bash bash

Bad substitution error in bash script


Try using:

#!/bin/bash

instead of

#! /bin/sh


The reason for this error is that two different shells are used in these cases.

$ . scp.sh command will use the current shell (bash) to execute the script (without forking a sub shell).

$ ./scp.sh command will use the shell specified in that hashbang line of your script. And in your case, it's either sh or dash.

The easiest way out of it is replacing the first line with #!/bin/bash (or whatever path bash is in).