Skip to content

Fix "bad substitution" in Bash: sh vs bash, Old Bash and ${} Typos

troubleshootingdashposixstring-manipulationshebang
7 min read

Quick Answer

The error Bad substitution means the shell found a ${...} expansion it does not support. The most common cause is running a bash script with sh script.sh: on Debian, Ubuntu and Kali, /bin/sh is dash, which ignores the #!/bin/bash shebang when called this way and rejects bash-only forms such as ${name^^}, ${name,,}, ${file/ /_}, ${file:0:1} and ${!var}. dash prints script.sh: 3: Bad substitution and stops the script with exit status 2. Run it as ./script.sh or bash script.sh instead, so the shebang picks the interpreter. The second cause is an old bash: 3.2, the version macOS ships, has no ${var^^} or ${var,,}. The third is a typo such as ${$var}, which is invalid in every shell; the indirect form is ${!var}. In bash the error skips only the failing line and the script keeps running, so add set -e. For sh-only scripts, use tr for case changes.

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:

text
$ cat up.sh #!/bin/bash name="travis" echo "${name^^}" echo "done" $ readlink -f /bin/sh /usr/bin/dash $ sh up.sh up.sh: 3: Bad substitution exit=2 $ bash up.sh TRAVIS done exit=0 $ chmod +x up.sh; ./up.sh TRAVIS done exit=0

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:

Expansionbash 5.3.15bash 4.0.44bash 3.2.57dash 0.5.12busybox 1.37 sh
${name^^}, ${name,,}worksworksbad substitutionBad substitutionbad substitution
${$var}bad substitutionbad substitutionbad substitutionBad substitutionbad substitution
${file/ /_}, ${file:0:1}worksworksworksBad substitutionworks
${ name} (space)unexpected EOFbad substitutionbad substitutionBad substitutionbad substitution
${var:-$(date +%Y}unexpected EOFunexpected EOFbad substitution: no closing )Unterminated quoted stringunterminated quoted string
script continues after a bad substitution?yes, exit 0yes, exit 0yes, exit 0no, exit 2no, exit 2

The raw output for the case-change line, which is the one people search for:

text
#### Kali bash 5.3.15 1 upper: TRAVIS 2 after exit=0 #### Kali dash cases.sh: 2: Bad substitution exit=2 #### bash:3.2 cases.sh: line 2: 1 upper: ${name^^}: bad substitution 2 after exit=0 #### busybox cases.sh: line 3: syntax error: bad substitution exit=2

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:

text
$ bash typo4.sh # bash 5.3.15 typo4.sh: line 2: unexpected EOF while looking for matching `"' exit=2 $ bash typo4.sh # bash:4.0 and bash:3.2 images typo4.sh: line 1: 1 space: ${ name}: bad substitution 2 after exit=0

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:

text
$ cat status.sh var=name echo "value: ${$var}" echo "status of that line: $?" $ bash status.sh status.sh: line 2: value: ${$var}: bad substitution status of that line: 1 exit=0

The script exits 0. With set -euo pipefail at the top, the same line ends it:

text
$ bash strict.sh strict.sh: line 3: value: ${$var}: bad substitution exit=1

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:

bash
#!/usr/bin/env bash
text
$ chmod +x up.sh && ./up.sh # or: bash up.sh TRAVIS done

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:

text
$ ./shebang-bin.sh # #!/bin/bash sh: ./shebang-bin.sh: not found exit=127 $ ./shebang-env.sh # #!/usr/bin/env bash 3.2.57(1)-release exit=0

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}:

text
$ cat fix-indirect.sh #!/usr/bin/env bash name="Travis" var=name echo "indirect: ${!var}" $ ./fix-indirect.sh indirect: Travis

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:

bash
#!/bin/sh name="Travis" file="a b.txt" upper=$(printf '%s\n' "$name" | tr '[:lower:]' '[:upper:]') lower=$(printf '%s\n' "$name" | tr '[:upper:]' '[:lower:]') under=$(printf '%s\n' "$file" | tr ' ' '_') echo "$upper $lower $under"
text
TRAVIS travis a_b.txt

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:

text
$ shellcheck sc.sh In sc.sh line 3: echo "${name^^}" ^-------^ SC3059 (warning): In POSIX sh, case modification is undefined. In sc.sh line 5: echo "${!var}" ^-----^ SC3053 (warning): In POSIX sh, indirect expansion is undefined. $ shellcheck sc2.sh In sc2.sh line 3: echo "${$var}" ^-----^ SC2082 (error): To expand via indirection, use arrays, ${!name} or (for sh only) eval.

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

PAID RESOURCE — $9

The Production Bash Toolkit

An operational script system + a 30-function shared library + a 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →
curl -O bashlib-starter.sh

Get the bashlib starter

Ten functions I source into every script on my own boxes — strict-mode setup, an ERR trap that names the failing line, lock and timeout wrappers, and cleanup that runs on every exit path. After that, a short email only when something breaks on a real box and I've worked out why, about twice a month. One-click unsubscribe.

BashSnippets logo

Written by Travis

Creator of BashSnippets.xyz

bashsnippets.xyz/about

Related Snippets

Frequently Asked Questions

faq — snippet

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.

faq — snippet

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.

faq — snippet

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.

faq — snippet

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.

faq — snippet

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.