Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 6 down vote accepted

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"

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

share|improve this answer

You must log in to answer this question.

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