Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows PATH to posix path conversion in bash

How can I convert a Windows dir path (say c:/libs/Qt-static) to the correct POSIX dir path (/c/libs/Qt-static) by means of standard msys features? And vice versa?

like image 550
Tomilov Anatoliy Avatar asked Dec 04 '12 10:12

Tomilov Anatoliy


People also ask

Where is bash path in Windows?

Go to File > Preferences > Settings and type shell in search settings. After that, navigate to Terminal > Integrated > Shell:Windows and update the path with Git Bash executable: C:\Program Files\Git\bin\bash.exe and save. Reopen VS Code terminal and it will prompt Git Bash.

What is Cygpath?

The cygpath program is a utility that converts Windows native filenames to Cygwin POSIX-style pathnames and vice versa. It can be used when a Cygwin program needs to pass a file name to a native Windows program, or expects to get a file name from a native Windows program.

How do I create a path in git bash?

Click on Advanced System Settings. Click on Environment Variables. Under System Variables, look for the path variable and click edit. Add the path to git's bin and cmd at the end of the string like this: ;C:\Program Files\Git\bin\git.exe;C:\Program Files\Git\cmd.


2 Answers

Cygwin, Git Bash, and MSYS2 have a readymade utility called cygpath.exe just for doing that.

 Output type options:   -d, --dos             print DOS (short) form of NAMEs (C:\PROGRA~1\)   -m, --mixed           like --windows, but with regular slashes (C:/WINNT)   -M, --mode            report on mode of file (binmode or textmode)   -u, --unix            (default) print Unix form of NAMEs (/cygdrive/c/winnt)   -w, --windows         print Windows form of NAMEs (C:\WINNT)   -t, --type TYPE       print TYPE form: 'dos', 'mixed', 'unix', or 'windows' 
like image 80
anishsane Avatar answered Sep 20 '22 22:09

anishsane


I don't know msys, but a quick google search showed me that it includes the sed utility. So, assuming it works similar in msys than it does on native Linux, here's one way how to do it:

From Windows to POSIX

You'll have to replace all backslashes with slashes, remove the first colon after the drive letter, and add a slash at the beginning:

echo "/$pth" | sed 's/\\/\//g' | sed 's/://' 

or, as noted by xaizek,

echo "/$pth" | sed -e 's/\\/\//g' -e 's/://' 

From POSIX to Windows

You'll have to add a semi-colon, remove the first slash and replace all slashes with backslashes:

echo "$pth" | sed 's/^\///' | sed 's/\//\\/g' | sed 's/^./\0:/' 

or more efficiently,

echo "$pth" | sed -e 's/^\///' -e 's/\//\\/g' -e 's/^./\0:/' 

where $pth is a variable storing the Windows or POSIX path, respectively.

like image 38
Rody Oldenhuis Avatar answered Sep 23 '22 22:09

Rody Oldenhuis