Sed error: bad flag in substitute command: 'U'


I'm new in bash script and trying to replace some words in my file using sed. Following is the bash I use in my script:

sed -i '' "s/<pre>.*<\/pre>/<pre>($NEWNAME) $MD5<\/pre>/"~/Desktop/replace.html

And I got error message saying: bad flag in substitute command: 'U'. I use double quote because I need to put variables in.

My environment is Mac.


1.Turns out I forgot to leave a space between replace string and file name. Which led to the result always showing: bad flag in substitute command: '~'. It works now.

2.The reason is I used MD5=$(md5 path) to create MD5 value which gets the reault of MD5 (path) *****, and the path contains / which breaks the regex. After changing MD5=$(md5 -q path), it will be ok.


Most likely your $NEWNAME variable has a forward slash in it, which is being used as regex delimiter in sed. Try this sed with an alternate delimiter e.g. ~:

sed -i '' "s~<pre>.*</pre>~<pre>($NEWNAME) $MD5</pre>~" ~/Desktop/replace.html

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at


Login to comment


Boxplot error with a substitute command that works fine with hist() and plot()

sed gives error with unterminated substitute in regular expression

Replacing Abbreviation strings (U.S.A.) to USA using sed command

Bad substitution error when passing decremented variable to "sed" command

why the ending g keyword in this sed substitute command is not matching all the occurrences?

Substitute with backslash in sed

"bad flag in substitute command: '{" while replacing a string with sed from one file to another file

Htaccess raised bad flag delimiters error

Error with sed on MAC OS X invalid command u

Substitute Column With Result of Command

Substitute a string containing && with sed

Sed substitute pattern with command

a command substitute

Error 500: .htaccess RewriteCond: bad flag delimiters

stringr: U_REGEX_BAD_INTERVAL error

Getting bad flag in substitute command '/' for sed replacement

How to use substitute command in unix(vi or sed) to achieve the following results

sed command works with e flag but not with E

sed s command & e flag: `echo '$-' | sed 's/-//e'` gives: `sh: $: command not found`

bash: bad flag in substitute command with escaped slashes

lookup command gets unknown flag error

sed throws 'bad flag in substitute command' but works fine in package.json

execute() error neovim with substitute command

sed select line and substitute string in one command

Sed command failing with bad flag in script

sed substitute with regex

sed command: bad flag in substitute command: '-'

How to fix Bad File descriptor in a Sed Command in unix

Is sed allowed to create a file if w command or flag is never executed?