1

I have a bash script containing this line:

dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

This runs fine on OSX.

However, when I move it move the script on a Docker container running Debian, it fails on the above line with this message:

Bad substitution

Any idea why this happens?

bsky
  • 19,326
  • 49
  • 155
  • 270
  • 2
    What's the shebang on the script? `#!/bin/sh` isn't bash. (Even when `/bin/sh` is implemented as a symlink to bash, it runs in POSIX mode, disabling many extensions to the standard). If you want full bash syntax, your shebang *must* be `#!/bin/bash`. – Charles Duffy Jan 18 '17 at 16:23
  • 1
    (Likewise, running a script with `sh yourscript` will interpret it with a shell only guaranteed to provide POSIX-baseline functionality, even if that script starts with `#!/bin/bash`). – Charles Duffy Jan 18 '17 at 16:29

1 Answers1

1

You are using dash shell, not bash.

Check with: ls -la /proc/$$/exe

Ipor Sircer
  • 3,069
  • 3
  • 10
  • 15
  • `ls -la /proc/$$/exe` has the result `lrwxrwxrwx 1 root root 0 Jan 16 17:20 /proc/6/exe -> /bin/bash`, so I'm not using `dash` – bsky Jan 18 '17 at 16:55
  • If using bash, then the safest thing to do would be to check if the script is called standalone, or sourced: – Shoham Feb 09 '17 at 10:50
  • if [ -z "${BASH_SOURCE}" ]; then SCRIPTPATH=$0 else SCRIPTPATH=${BASH_SOURCE[0]} fi – Shoham Feb 09 '17 at 10:51