Skip to content

/errors/sed/unterminated-s-command

sed: -e expression #1, char N: unterminated `s' command

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · sed (GNU sed) 4.9updated 2026-10-20
tool
sed
exit code
1
versions
GNU sed 4.x
os
linux, windows

GNU sed fails with sed: -e expression #1, char N: unterminated `s' command when an s (substitute) command does not have all three delimiters, as in s/old/new. The most common cause is a / inside a path or URL that ends the command early. char N is the position where sed gave up.

Reproduce the error

bash
echo "hello" | sed 's/hello/world'

Output · exit 1

sed: -e expression #1, char 13: unterminated `s' command

A slash inside the replacement fails too, with a related message: sed takes the part after the third slash as flags, and s and r are not valid flags:

bash
echo "root=/var/www" | sed 's/\/var\/www//srv/app/'

Output · exit 1

sed: -e expression #1, char 15: unknown option to `s'

Fix: close the command, or change the delimiter

Any character after s becomes the delimiter, so pick one that is not in your text:

bash
echo "hello" | sed 's/hello/world/'
echo "path=/usr/local/bin" | sed 's|/usr/local|/opt|'
echo "path=/usr/local/bin" | sed 's/\/usr\/local/\/opt/'

Output

world
path=/opt/bin
path=/opt/bin

With shell variables

When the replacement comes from a variable, a slash inside it breaks the command at run time. Use a delimiter that cannot appear in the value:

bash
new_dir="/srv/app/releases"
echo "root=/var/www" | sed "s|/var/www|$new_dir|"

Output

root=/srv/app/releases

Sources