Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping backslash in windows paths passed to unix programs

Tags:

bash

cygwin

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.

Ted

like image 558
Flethuseo Avatar asked Nov 01 '11 04:11

Flethuseo


People also ask

Does Windows use backslash for paths?

Windows traditionally uses the backslash ( \ ) to separate directories in file paths. (For example, C:\Program Files\PuppetLabs .)

Why does Windows use backslash and Linux forward slash?

Even if you're running a web server or FTP server on a Windows machine, they'll use forward slashes because that's what the protocol calls for. Other operating systems use forward slashes for the same reason — it's the Unix convention. Linux is a Unix-like operating system, so it uses the same type of slash.

Does Unix use forward slash or backslash?

You can use forward slashes ( / ) instead of backward slashes ( \ ) on Windows (as is the case with Linux® and UNIX). If you use backward-slashes, the file name on Windows might be treated as a relative-path instead of an absolute-path.

How do you escape a path character?

About Escaping File Paths Characters to be escaped include the backslash (\, because it is the escaping character) and the double quotes (").


1 Answers

Sometimes it is useful to give manuals of the used programs a try :-) . From man xargs:

OPTIONS
   -0, --null
          Input items are terminated by a null character instead  of  by
          whitespace,  and the quotes and backslash are not special (ev-
          ery character is taken literally).  Disables the end  of  file
          string, which is treated like any other argument.  Useful when
          input items might contain white space, quote marks,  or  back-
          slashes.   The GNU find -print0 option produces input suitable
          for this mode.

This option indeed preserves the whole line as is. Try:

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

The "-0" does it for you!

Note that for more than one arg you must zero-terminate the arguments, for example by find ... -print0 or grep ... --null.

like image 156
newby2000 Avatar answered Oct 18 '22 09:10

newby2000