I've been trying to compile some ocaml packages and keep running into some errors of the form sh: 1: Bad substitution (exit code 2), eg.

/bin/sh -exc echo | m4
/bin/sh: 1: Bad substitution

But, /bin/sh -ec echo | m4 has no error. It seems that any command with both -e and -x flags passed to /bin/sh (/bin/sh: symbolic link to dash) results in this error. Is this the expected behaviour, because I can't see anything to that effect in the manual, and I can't remember ever having this happen before.

EDIT: actually I see the error with any /bin/sh -xc command, regardless of -e

Ubuntu 18.04


I finally found the problem. In my ~/.bashrc I set

export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

Which the dash manual says gets output before every trace line when -x is set. Exporting PS4 without bashisms solves the problem.

I hadn't been expecting my ~/.bashrc to be read at all in this case, but since bash is being used to execute /bin/sh I assume those variables are being seen by dash as well, annoyingly.

| improve this answer | |
  • It's being seen by the child shell because you're exporting it: PS1 thru PS4 should really be plain shell variables (not exported) for exactly the reason you have just discovered i.e. that they are not portable between shells – steeldriver Sep 4 '19 at 2:13
  • @steeldriver yea, that was a frustating bug that caused me to make my entire config posix compliant before realizing this. Not exporting PS4 seems like a bummer, though, when debugging. I was thinking about asking a follow-up about that – Rorschach Sep 4 '19 at 2:16
  • So why does running /bin/sh source bashrc ?have you symlinked /bin/sh to /bin/bash instead of /bin/dash ? – Sergiy Kolodyazhnyy Sep 4 '19 at 5:03
  • @SergiyKolodyazhnyy no, it's because I use bash as my default shell and was running build scripts from it – Rorschach Sep 4 '19 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.