开发者

How to format a Windows path to a Unix path on Cygwin command line

开发者 https://www.devze.com 2022-12-26 17:08 出处:网络
When using Cygwin, I frequently copy a Windows path and manually edit all of the slashes to Unix format.For example, if I am using Cygwin and need to change directory I enter:

When using Cygwin, I frequently copy a Windows path and manually edit all of the slashes to Unix format. For example, if I am using Cygwin and need to change directory I enter:

cd C:\windows\path 

then edit this to

cd C:/windows/path  

(Typically, the path is much longer than that). Is there a way to use sed, or something else to do this automatically? For example, I tried:

echo C:\windows\path|sed 's|\\|g'  

but got the following error

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

My goal is to reduce the typing, so maybe I could write a program which I could call. Ideally I would type:

开发者_如何学GoconversionScript cd C:/windows/path 

and this would be equivalent to typing:

cd C:\windows\path


Thanks all. Apparently all I need are single quotes around the path:

cd 'C:\windows\path'

and Cygwin will convert it. Cygpath would work too, but it also needs the single quotes to prevent the shell from eating the backslash characters.


Read about the cygpath command.

somecommand `cygpath -u WIN_PATH`

e.g.


cmd.exe doesn't like single quotes. You should use double quotes

C:\test>echo C:\windows\path|sed "s|\\|/|g"
C:/windows/path


You replace back-slash by slash using unix sed

Below I use star "*" to seperate fields in s directive


     sed "s*\\\*/*g"

The trick is to use one back-slash more than you might think needed


to answer your question to achieve

cd C:\windows\path

since you are in bash this just works as you want - but add single quotes

cd 'C:\windows\path'

As noted by @bmargulies and @Jennette - cygpath is your friend - it would be worth it to read the cygwin man page

man cygpath

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号