How to customize the ‘which’ command of Cygwin to display paths in Windows’ style

This post explains how to customize the which command in Cygwin to print Windows-style paths.

The which command in UNIX shows the full path of (shell) commands. That is, it displays the locations of the executables.

The built-in which command in Cygwin displays the path in UNIX-style.

>which cmd.exe
/cygdrive/c/Windows/system32/cmd.exe

To convert a path from UNIX-style to Windows-style or vice-versa, the cygpath command of Cygwin can be used.

>cygpath -w /cygdrive/c/Windows/system32/cmd.exe
C:\Windows\system32\cmd.exe

If we want the which command to always display the paths in Windows style (without explicitly using cygpath every time) we can create a custom function in .bashrc file. The steps are as follows:

1. Start Cygwin command-line interface

2. Edit the ~/.bashrc file

>vi ~/.bashrc

3. Add the following lines in the .bashrc file

function which() {
cygpath -w $(/usr/bin/which $1)
}

This creates a custom function with the name which. The custom function calls the built-in which command and feed its output to the cygpath command. The -w option instructs the cygpath command to display the path in Windows’ style.

4. Save the modified .bashrc file and re-start the Cygwin command-line interface.

Now, the command which will actually call the custom function that we defined (instead of the built-in command) and display the paths in Windows’ style.

>which cmd.exe
C:\Windows\system32\cmd.exe

Advertisements
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s