Syntax error: redirection unexpected when a Bash script runs under sh
Syntax error: redirection unexpectedShort 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.
Fix it fast
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.sh2Match what you see, then jump to its fix
- 1
The first line is #!/bin/sh and the failing line contains <<< or &>.The shebang says /bin/sh but the script uses Bash syntaxFix - 2
The 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
The 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
- 1
What does the error look like?
Syntax error: redirection unexpectedscript.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.sh2Fix
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.shConfirmed by gnu.org
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/null2Fix
Call the script with bash, or run it directly.
bash script.sh
# or
./script.sh3Verify
The same command now runs past the failing line.
bash script.shConfirmed by manpages.debian.org
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.shConfirmed by manpages.debian.org
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
Servers
apt-get does not exist on this EC2 instance
sudo: apt-get: command not found
apt-get belongs to Debian and Ubuntu.
2 causes, each with a fixServers
bash warns that it cannot change the locale after you log in to a server
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
The shell was told to use a locale that is not built on the server.
3 causes, each with a fixServers
ping is missing inside a Docker container
sh: 1: ping: not found
Base images such as ubuntu are minimal, so ping is simply not installed.
2 causes, each with a fixServers
Running ps inside a container says command not found
bash: ps: command not found
Slim Debian and Ubuntu images do not ship the procps package, and ps comes from procps.
2 causes, each with a fixServers
GitHub Actions step fails writing to GITHUB_OUTPUT
Error: Invalid format '...'
The runner reads GITHUB_OUTPUT line by line, and every entry must be name=value or a name<<DELIMITER block.
3 causes, each with a fixServers
pip install worked but the jupyter command is not found
bash: jupyter: command not found
The shell can only run programs that sit in a folder on PATH.
2 causes, each with a fix