3

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

anjanbacchu
  • 1,457

1 Answers1

3

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.