8

I'm using Windows and writing shell scripts to run on Babun (a POSIX api).

I need to read a path from an environment variable, $USERPROFILE, and convert it to unix style (replace \ by /, c:\ by /c/) so I can use it later in the script.

The problem is whenever I echo the content of $USERPROFILE, the backslashes are interpreted as escaping characters and I get an unexpected result (letters "randomly" turn into special characters like \n, \f, etc). So I can't echo it into sed for example.

1 Answer 1

11

Cygwin provides the very useful cygpath utility for converting Unix and Windows format paths. From the Cygwin User Guide

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. Alternatively, cygpath can output information about the location of important system directories in either format.

It can convert to/from either Windows or DOS-style format paths, e.g. to print the Unix format of a Windows path:

$ cygpath -u "$USERPROFILE"
/cygdrive/c/Users/anthony

See the Cygwin User Guide for full details and cygpath --help for basic usage information.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .