Windows PATH to posix path conversion in bash

33,547

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

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,

  1. If you're using the git-bash shell, then yes.
  2. If you're in cygwin, then yes.
  3. If you're in another shell, but you have installed git-bash before, then cygpath can be found at git-bash-install-folder\usr\bin\cygpath.exe.
  4. Else: maybe not, but I'm sure you can find a way to installed it.

Solution 3

The "correct" way in MSYS is:

$ MSYS_NO_PATHCONV=1  taskkill /F /T /IM ssh-agent.exe

This avoids having to manually translate slashes. It simply de-activates the path conversion.

Solution 4

Here is my implementation (tested on git bash).

From POSIX to Windows

sed '
    \,/$, !s,$,/,
    \,^/, s,/,:/,2
    s,^/,,
    s,/,\\,g
    ' <<< "$@"

Works for:

/c/git
relative/dir
c:/git
~
.
..
/c
/c/
./relative/dir
/sd0/some/dir/

except

/
<path with space>

Explanation:

\,^/, s,/,:/,2 (converts /drive/dir/ to /drive:/dir/) is the heart of it and inserts : before the 2nd /. I use , for delim instead of / for readability. If starting with / (\,^/,), then replace / with :/ for the 2nd occurrence. I do not want to assume drive letter length of 1 so this works for /sd0/some/dir.

s,^/,, removes the leading / and s,/,\\,g converts all / to \.

\,/$, !s,$,/, is to handle the corner case of /c and ensure 2nd / (/c/) for the next command to work.

Note:

If here string <<< does not work in your shell then you can echo and pipe as

echo "$@" | sed ...

Errata

Here e script

Solution 5

just FYI - at least for my git version 2.26.2.windows.1 e.g. if I have a path like C:\dev\work_setup\msk, I can go directly to Git Bash and type

cd "C:\dev\work_setup\msk"

this will result in current folder being changed to /c/dev/work_setup/msk - so this type of conversion seems to be done automatically, as long as I put the Windows path inside double quotes. Unfortunately I don't have references to original documentation that would back that up.

Share:
33,547
Tomilov Anatoliy
Author by

Tomilov Anatoliy

В течение 24 часов мне предписано покинуть этот прекрасный мир. Что ж, опять прыгать с трамплина…

Updated on July 23, 2022

Comments

  • Tomilov Anatoliy
    Tomilov Anatoliy almost 2 years

    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?