1

I am getting a bad substitution error for the following line:

cur_hour=${cur_hour:11:2}

The relevant code is here:

while :
do
    cur_hour=`date`
    cur_hour=${cur_hour:11:2}

    if [ <some comparisons> ]
    then
        <some other code here>
    fi
done

When I try the "trouble" code manually it works fine but in the script it causes the error. Any help is appreciated.

  • How are you running the script? This kind of error happens most commonly when someone writes a script for bash, but runs it with `/bin/sh`. – kojiro Jan 09 '16 at 22:26
  • `./loop.sh`, where "loop.sh" is the file name –  Jan 09 '16 at 22:27
  • Is this the entire script? If not, what is the very first line of the script file? – kojiro Jan 09 '16 at 22:28
  • 3
    @JDOdle Make sure the first line is `#!/bin/bash`. It can not be `#!/bin/sh` – that other guy Jan 09 '16 at 22:28
  • i do have it as `#!/bin/sh` what is the difference here? –  Jan 09 '16 at 22:29
  • sh is a subset of bash, it does not have the same features as bash. Change the shebang line to `#!/bin/bash` and you should be golden – G. Grasso Jan 09 '16 at 22:32
  • Incidentally, if you read the StackOverflow tag wiki for bash, this is one of the things you're supposed to check for before you ask a question here. :) – Charles Duffy Jan 09 '16 at 22:33
  • 1
    ...for future reference: http://stackoverflow.com/tags/bash/info – Charles Duffy Jan 09 '16 at 22:33
  • Oh, by the way, it's not _merely_ the shebang here; if /bin/sh were a symlink to /bin/bash this particular error would not have happened, because even in POSIX mode bash still implements the substring parameter expansion. To get this particular error, /bin/sh has to be implemented by something other than bash (most likely dash). – kojiro Jan 09 '16 at 22:38

0 Answers0