Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Syntax error: redirection unexpected when a Bash script runs under sh

ErrorSyntax error: redirection unexpected

Short answer

The operators <<< (here-string) and &> are Bash features. dash, the POSIX /bin/sh on many Linux systems, does not list them, so a script run by sh stops with 'redirection unexpected'. Run the script with bash (shebang or command), or rewrite the line with POSIX redirections.

  • Applies to Bash 5.2, dash, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See which shell reads the script and which one /bin/sh is. The line number in the error is the first Bash-only line.

    head -n 1 script.sh
    readlink -f /bin/sh
    sed -n '12p' script.sh
  2. 2Match what you see, then jump to its fix

    1. 1The first line is #!/bin/sh and the failing line contains <<< or &>.The shebang says /bin/sh but the script uses Bash syntaxFix
    2. 2The shebang is #!/bin/bash, yet the error appears when the script is launched as sh script.sh, or from a CI step, a Dockerfile RUN line or a cron entry that names sh.The script is started with sh, even though it has a Bash shebangFix
    3. 3The script is meant for /bin/sh (a container with only dash, a system installer, a tiny image) and Bash is not available.The script must stay POSIX, so the line needs rewritingFix

What does the error look like?

  • Syntax error: redirection unexpected
  • script.sh: 12: Syntax error: redirection unexpected

Cause 1 of 3

The shebang says /bin/sh but the script uses Bash syntax

1Check

Here-strings use <<< and the combined redirect uses &>. The Bash manual documents both; the dash manual lists << but not <<<, and lists >& but not &>.

grep -nE '<<<|&>' script.sh

2Fix

Change the shebang so Bash reads the script.

#!/usr/bin/env bash
read -r first rest <<< "hello world"

3Verify

Run the script directly, not with sh.

chmod +x script.sh
./script.sh

Confirmed by gnu.org

Did this fix it?

Cause 2 of 3

The script is started with sh, even though it has a Bash shebang

1Check

A shebang is ignored when you pass the file to a named interpreter. sh then reads it as POSIX shell.

grep -rn "sh script.sh\|sh -c" Makefile .github Dockerfile /etc/cron.d 2>/dev/null

2Fix

Call the script with bash, or run it directly.

bash script.sh
# or
./script.sh

3Verify

The same command now runs past the failing line.

bash script.sh

Confirmed by manpages.debian.org

Did this fix it?

Cause 3 of 3

The script must stay POSIX, so the line needs rewriting

1Check

The redirections dash lists are < > >| << >> <& >& <<- <>. A here-document and 2>&1 use only those.

command -v bash || echo "bash is not installed"

2Fix

Replace &> with a plain redirect plus 2>&1, and the here-string with a here-document or a pipe.

# Bash-only
my_cmd &> out.log
read -r first rest <<< "hello world"

# POSIX
my_cmd > out.log 2>&1
echo "hello world" | { read -r first rest; echo "$first"; }

3Verify

Run the script with sh explicitly.

sh script.sh

Confirmed by manpages.debian.org

Did this fix it?

How do I stop it coming back?

  • Put the right shebang on every script: #!/usr/bin/env bash for Bash features, #!/bin/sh only for plain POSIX.
  • Run scripts with ./script.sh in CI so the shebang decides the shell.
  • Test POSIX scripts with sh script.sh before shipping them.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.