sed fails with "unknown option to `s'" error

sed
Mr. King picture Mr. King · Feb 20, 2012 · Viewed 110.5k times · Source

I'm trying to use

sed -i -e "s/.*seb.*/    \"$ftp_login_template\"/" $ftp_dir

however I get this error:

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

I don't understand why since this works perfectly:

sed -i -e "s/.*wbspassword.*/    \"wbspassword\": \"$password\",/" $user_conf

Any ideas as to what I'm doing wrong?

Could this be the problem? ftp_login_template=\${user}:${password}:24:86::\/var\/lib\/clit.${user}\/downloads:\/bin\/false\"

Answer

Tomas Pruzina picture Tomas Pruzina · Feb 20, 2012

The problem is with slashes: your variable contains them and the final command will be something like sed "s/string/path/to/something/g", containing way too many slashes.

Since sed can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn't appear in your replacement string:

replacement="/my/path"
sed --expression "s@pattern@$replacement@"

Note that this is not bullet proof: if the replacement string later contains @ it will break for the same reason, and any backslash sequences like \1 will still be interpreted according to sed rules. Using | as a delimiter is also a nice option as it is similar in readability to /.