Look at the shape of the message first
script.sh: 3: Bad substitution (capital B, no "line") is dash: the script ran under sh. script.sh: line 3: …: bad substitution is bash, and the cause is the expansion itself or an old bash.
The script works on your laptop and dies in a Dockerfile RUN sh deploy.sh, a CI step or a cron line that calls sh. The #!/bin/bash at the top did not help, because sh script.sh never reads it. And when bash itself is the one complaining, it skips the line and carries on, so the script can finish with exit 0 having done half its work.
Why Does sh script.sh Say Bad substitution?
Reproduced on 2026-11-03 on Kali 2026.3 (bash 5.3.15, dash 0.5.12), with a four-line script:
sh up.sh asks for /bin/sh by name, and on Debian, Ubuntu and Kali that is dash. When you name the interpreter on the command line, the kernel never looks at the shebang, so #!/bin/bash is a comment. dash does not have ${var^^}, reports Bad substitution and stops: done never prints. The same three commands in docker run --rm ubuntu:24.04 (dash 0.5.12-6ubuntu5) and ubuntu:26.04 (dash 0.5.12-12ubuntu3) gave identical output.
bash --posix up.sh printed TRAVIS: POSIX mode does not remove bash's expansions. So on a system where /bin/sh is bash, sh up.sh works, and the bug only shows up on a machine where it is dash.
Which ${} Forms Fail, and What Does Each Shell Print?
Each line run as a separate script with a second echo "2 after" line, under bash 5.3.15 and dash 0.5.12 on Kali, and the bash:3.2, bash:4.0 and busybox:1.37 Docker images:
| Expansion | bash 5.3.15 | bash 4.0.44 | bash 3.2.57 | dash 0.5.12 | busybox 1.37 sh |
|---|---|---|---|---|---|
${name^^}, ${name,,} | works | works | bad substitution | Bad substitution | bad substitution |
${$var} | bad substitution | bad substitution | bad substitution | Bad substitution | bad substitution |
${file/ /_}, ${file:0:1} | works | works | works | Bad substitution | works |
${ name} (space) | unexpected EOF | bad substitution | bad substitution | Bad substitution | bad substitution |
${var:-$(date +%Y} | unexpected EOF | unexpected EOF | bad substitution: no closing ) | Unterminated quoted string | unterminated quoted string |
| script continues after a bad substitution? | yes, exit 0 | yes, exit 0 | yes, exit 0 | no, exit 2 | no, exit 2 |
The raw output for the case-change line, which is the one people search for:
Bash 3.2.57 is the same release macOS still ships as /bin/bash, so a script that uses ${name^^} works on Linux and fails on a stock Mac with the bash-style message. busybox reported line 3 for an error on line 2 in every case here, so do not trust its line number.
The surprise: bash 5.3 does not say bad substitution for ${ name} any more. Bash 5.3 added ${ command; } (run a command in the current shell and substitute its output), so a stray space after ${ now starts a command substitution and the parser runs to the end of the file looking for the close:
Same typo, different message and the opposite exit status, depending on the bash version. If a bash 5.3 script reports unexpected EOF on its last line, look for ${ with a space.
Does bad substitution Stop the Script?
In bash, no. The failing command gets status 1 and the next line runs:
The script exits 0. With set -euo pipefail at the top, the same line ends it:
bash 3.2.57 behaved the same both ways. dash and busybox always stop with exit 2, set -e or not.
How Do I Fix bad substitution?
1. Run a bash script with bash. Put the interpreter in the shebang, make the file executable and run it by path:
In a Dockerfile, CI step or crontab, write bash deploy.sh or ./deploy.sh, never sh deploy.sh. Prefer #!/usr/bin/env bash to #!/bin/bash: in the bash:3.2 image there is no /bin/bash at all (bash is /usr/local/bin/bash), and the two shebangs ran like this:
The same applies on a Mac with Homebrew bash: env finds the newer bash first in PATH, /bin/bash is always 3.2.
2. Fix the typo. ${$var} is invalid everywhere. For indirect expansion bash uses ${!var}:
It ran the same on bash 3.2.57. A missing ) or } inside ${…} needs the bracket, not a different shell.
3. If the script must run under sh, use portable tools. When the target really is /bin/sh (a minimal container, an init script, busybox), replace the bash expansions with tr:
Identical output under dash 0.5.12, busybox 1.37 sh and bash 5.3.15. The string manipulation page has the bash-only forms side by side.
Does ShellCheck Catch It?
Yes, if the shebang tells it which shell the script is for. ShellCheck 0.11.0 on a #!/bin/sh script using bash expansions, and on a #!/bin/bash script with the ${$var} typo:
ShellCheck cannot see sh script.sh in a Dockerfile or crontab: it checks the script against its own shebang. A #!/bin/bash script with ${name^^} is clean, and that is the case that fails at run time. Grep your Dockerfiles, CI configs and crontabs for sh followed by a .sh file. Paste other findings into the ShellCheck Error Decoder.
Prerequisites
Tested on Kali 2026.3 (bash 5.3.15, dash 0.5.12-12, ShellCheck 0.11.0) and in Docker: ubuntu:24.04 (dash 0.5.12-6ubuntu5), ubuntu:26.04 (dash 0.5.12-12ubuntu3), bash:3.2 (3.2.57), bash:4.0 (4.0.44) and busybox:1.37. No root.
Frequently Asked Questions
Why does my script work with bash but say Bad substitution with sh?
Because sh is not bash on Debian, Ubuntu or Kali. /bin/sh points to dash, a smaller POSIX shell. Running sh script.sh starts dash directly and the #!/bin/bash line is treated as a comment, so bash-only expansions like ${name^^} fail with Bad substitution and exit status 2. Run the script as ./script.sh after chmod +x, or as bash script.sh.
How do I uppercase or lowercase a string in sh without ${var^^}?
Use tr, which works in dash, busybox sh and every bash: upper=$(printf '%s\n' "$name" | tr '[:lower:]' '[:upper:]') and the reverse for lowercase. The same approach replaces ${file/ /_}: printf '%s\n' "$file" | tr ' ' '_'. All three ran identically in dash 0.5.12, busybox 1.37 and bash 5.3.
Does bash 3.2 support ${var^^} and ${var,,}?
No. Case modification arrived in bash 4.0. In the bash:3.2 Docker image (bash 3.2.57, the same release macOS ships as /bin/bash), ${name^^} prints line 2: 1 upper: ${name^^}: bad substitution, and the bash:4.0 image prints TRAVIS. On a Mac, install a current bash with Homebrew and use #!/usr/bin/env bash so the newer one is found first in PATH.
Does bad substitution stop a bash script?
Not by default. Bash prints the error, gives the failing command status 1 and runs the next line, so the script can finish with exit 0 after skipping work. dash and busybox sh stop the script with exit status 2. Add set -e (or set -euo pipefail) to a bash script and the same error ends it with exit 1.
What is the correct way to write ${$var}?
In bash, ${!var} expands the variable whose name is stored in var: with name=Travis and var=name, ${!var} prints Travis. ${$var} is a bad substitution in every shell, and ShellCheck flags it as SC2082. In plain sh there is no indirect expansion; use eval carefully or restructure the script.
The safe bash script template starts every script with #!/usr/bin/env bash and set -euo pipefail for these reasons, and The Production Bash Toolkit ships that template ShellCheck-clean.
Part of the bash snippets collection
Related Scripts
- Bash String Manipulation —
${var^^},${var/ /_}and the other bash-only expansions - Fix "bash: command not found" — the other error a wrong interpreter or a broken shebang produces
- Fix "bad interpreter: /bin/bash^M" — when the shebang itself is the problem
- Bash Error Handling —
set -euo pipefail, which turns this error from skipped into fatal - Bash Error Messages, Decoded — the common bash errors, each with the one check that names the cause