/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
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:
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:
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:
new_dir="/srv/app/releases"
echo "root=/var/www" | sed "s|/var/www|$new_dir|"Output
root=/srv/app/releases
Sources
Related pages
compat · sedsed -i on macOS vs Linux: GNU sed vs BSD sedGNU sed takes -i with an optional attached suffix, BSD sed on macOS needs an argument: sed -i '' on macOS, sed -i on Linux. Other GNU vs BSD sed differences.cheat sheets · awkawk cheat sheet: fields, patterns, sums and printfawk cheat sheet: print fields, change the separator, filter rows by pattern or condition, sum and count columns, use BEGIN and END, printf formats and arrays.errors · findfind: paths must precede expressionfind reports paths must precede expression when an unquoted glob such as *.txt is expanded by the shell into several file names. Quote it: find . -name '*.txt'.