Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

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

Writer Andrew Henderson

I keep switching between my command prompt and my git bash shell on windows 10. Suppose I copied my current directory to the clipboard in my command prompt(cmd.exe) and want to switch to that directory in my git bash, I wrote a function d2u() in my .bashrc file which converted the windows path to git bash path and executed it, that worked just fine.

anjan@DESKTOP-RPUVCRE MINGW64 /d
$ cat /dev/clipboard
D:\work\playground\reactNative
anjan@DESKTOP-RPUVCRE MINGW64 /d
$ type d2u
d2u is a function
d2u ()
{ cat /dev/clipboard | sed -r "s'\\\\'/'g" | sed -r "s'^D:'/d'g" | sed -r "s'^C:'/c'g"
}
anjan@DESKTOP-RPUVCRE MINGW64 /d
$ pushd `d2u`
/d/work/playground/reactNative /d /d/work/playground/reactNative /d /d/work/playground/reactNative ~
anjan@DESKTOP-RPUVCRE MINGW64 /d/work/playground/reactNative

However, when I incrementally improved that to pushd to that unixy path in a new function cd2u, it bombs!

anjan@DESKTOP-RPUVCRE MINGW64 /d/work/playground/reactNative
$ type cd2u
cd2u is a function
cd2u ()
{ pushd `cat /dev/clipboard | sed -r "s'\\\\'/'g" | sed -r "s'^D:'/d'g" | sed -r "s'^C:'/c'g" `
}
anjan@DESKTOP-RPUVCRE MINGW64 /d/work/playground/reactNative
$ cd2u
sed: -e expression #1, char 7: unterminated `s' command
/d /d/work/playground/reactNative /d/work/playground/reactNative /d /d/work/playground/reactNative ~
anjan@DESKTOP-RPUVCRE MINGW64 /d
$

what gives ? why do I get this error ?

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

My sed version is relatively new

$ sed --version sed (GNU sed) 4.8

1 Answer

It's because backticks (``) interpret backslashes. This way your \\\\ in backticks work like \\ without backticks. Then they get to sed as \, because double-quoting also interprets backslashes. And ten sed interprets \' as escaped '.

Use $() instead of backticks. This syntax won't introduce additional level of interpreting backslashes. Nowadays $() should be your choice anyway.

And quote (hint: double-quoting $() will not interfere with whatever quotes you have inside $()).

And leave the cat alone.

Note: I'm neither reviewing nor going to review your sed expressions. They may be right for what you want to do, or wrong, or sub-optimal. I'm just explaining how moving backticks from the invocation of the function to the function body caused the error.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy