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
Windows traditionally uses the backslash ( \ ) to separate directories in file paths. (For example, C:\Program Files\PuppetLabs .)
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.
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.
About Escaping File Paths Characters to be escaped include the backslash (\, because it is the escaping character) and the double quotes (").
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With