bashcygwin

Escaping backslash in windows paths passed to unix programs


I am trying to escape backslashes in cygwin, but it seems almost impossible I have tried a lot of things, but none work right..

  echo "C:\Users\Ted\Documents\Unix\Scripts" | xargs echo
  echo 'C:\Users\Ted\Documents\Unix\Scripts' | xargs echo

More specifically I need to get a command to receive input in bash without losing the backslash characters. every time I try to pass an argument, the backslashes always disappear, destroying my input. And I don't know how I can tell it to just leave the backslashes on the input alone.

I have tried the following but neither seems work

  alias cyg0='cygpath '$*'  '
  alias cyg1='cygpath "$*"  '
  alias cyg2='cygpath "'$*'"'

  alias cyg3='cygpath '$@'  '
  alias cyg4='cygpath "$@"  '
  alias cyg5='cygpath "'$@'"'


  Ted@Machine01 ~
  $ cyg0 C:\Users\Ted\Music\Enigma
  C:UsersTedMusicEnigma

  Ted@Machine01 ~
  $ cyg1 C:\Users\Ted\Music\Enigma
  cygpath: can't convert empty path

  Ted@Machine01 ~
  $ cyg2 C:\Users\Ted\Music\Enigma
  cygpath: can't convert empty path

  Ted@Machine01 ~
  $ cyg3 C:\Users\Ted\Music\Enigma
  C:UsersTedMusicEnigma

  Ted@Machine01 ~
  $ cyg4 C:\Users\Ted\Music\Enigma
  C:UsersTedMusicEnigma

  Ted@Machine01 ~
  $ cyg5 C:\Users\Ted\Music\Enigma
  cygpath: can't convert empty path

By the way, I want to be able to type C:\Users\Ted\Music\Enigma without quotes. One of those aliases works when using quotes.


Solution

  • Please read the section titled QUOTING in bash(1) (man bash).

    In your first example,

    echo "C:\Users\Ted\Documents\Unix\Scripts" | xargs echo
    

    the shell interprets (and then removes) backslashes within double quotes, so the first echo command only sees the string C:UsersTedDocumentsUnixScripts.

    In your second example,

    echo 'C:\Users\Ted\Documents\Unix\Scripts' | xargs echo
    

    the single quotes correctly protect the backslashes, as you can see by running just the first echo command, without the | xargs echo.

    In all of your remaining examples,

    cyg0 C:\Users\Ted\Music\Enigma
    cyg1 C:\Users\Ted\Music\Enigma
    ...
    

    since the argument (C:\Users\Ted\Music\Enigma) is unquoted, once again the shell interprets and removes the backslashes before the command (cyg0, cyg1, ...) ever sees them. And...

    I want to be able to type C:\Users\Ted\Music\Enigma without quotes.

    Sorry, it can't be done. If you don't put the backslashes in single quotes, the shell will interpret each one as a quoting character for the character that follows it, then remove the backslash.

    You have a few options:

    1. Use single quotes on the command line, to protect the backslashes:

      cyg4 'C:\Users\Ted\Music\Enigma'
      
    2. Quote each backslash character separately, by doubling it, e.g.

      cyg4 C:\\Users\\Ted\\Music\\Enigma
      
    3. Use forward slashes instead:

      cyg4 C:/Users/Ted/Music/Enigma
      

    Option 3 could really be your best solution. Unfortunately the backslash character is just a very special character for all Unix shells, and this causes a lot of quoting hassles when dealing with DOS/Windows paths. But what's not widely known is that DOS/Windows is also perfectly happy to use the forward slash (/) as its path separator. Try it.

    Note: Of your several cyg* functions, only cyg1 and cyg4 are correct. The others use incorrect quoting. cyg1 is only useful with one argument since it joins all of the arguments together into one quoted string, while cyg4 can accept and pass on multiple arguments to cygpath. However, since cyg4 only passes on its quoted arguments, it doesn't add any value; it's really no different from just e.g.

    cygpath C:/Users/Ted/Music/Enigma
    

    Good luck.