0

I have a bash script my_script.sh as follows:

#!/bin/bash
echo "Hello There"
# declare arrays
declare -a my_array

When I run it using sh my_script.sh I get:

Hello There
': not a valid identifiermy_array

When I run it using /bin/bash my_script.sh I get:

Hello There
': not a valid identifiermy_array

How do I run this script?

Harry Boy
  • 4,159
  • 17
  • 71
  • 122
  • 1
    You've got CRLFs -- DOS newlines. It's not specific to arrays -- that'll cause _lots_ of things to fail. – Charles Duffy Mar 26 '21 at 22:31
  • BTW, the giveaway that that's the problem is that a carriage return, when printed to the terminal, causes the cursor to go to the leftmost column and start overwriting previously-printed text. – Charles Duffy Mar 26 '21 at 22:54
  • (Also, any script that uses bash-only features should only be run with `bash`, not `sh`; even when sh is a symlink to bash, it turns off some features for closer POSIX compliance when invoked under that name). – Charles Duffy Mar 26 '21 at 22:55

0 Answers0