Windows PATH to posix path conversion in bash
Solution 1:
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'
Solution 2:
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.
Solution 3:
Just use cygpath
:
$ cygpath -w "/c/foo/bar"
-> C:\foo\bar
$ cygpath -u "C:\foo\bar"
-> /c/foo/bar
You may wonder: "Do I have cygpath
installed?" Well,
- If you're using the git-bash shell, then yes.
- If you're in cygwin, then yes.
- If you're in another shell, but you have installed git-bash before, then
cygpath
can be found atgit-bash-install-folder\usr\bin\cygpath.exe
. - Else: maybe not, but I'm sure you can find a way to installed it.