Speaking UNIX

Opening Windows with Cygwin

Cygwin provides a complete UNIX shell—from awk to zcat—on Windows


Content series:

This content is part # of # in the series: Speaking UNIX

Stay tuned for additional content in this series.

This content is part of the series:Speaking UNIX

Stay tuned for additional content in this series.

If you're a regular reader of this column, you've likely now mastered much of the UNIX lingo. Colloquialisms such as "pipe the output," "kill the process," and "use a wildcard"—strange phrases to the uninitiated—should be familiar and comfortable.

Alas, UNIX isn't the only tongue spoken in the machine room. IBM® mainframe folks speak cants of "z," such as IBM z/OS® and System z9 Virtual Machine (z/VM); harried embedded systems developers jabber in eCos; and still others chit-chat in various argots of UNIX, such as Linux®, FreeBSD, Sun Solaris, and Mac OS X. Modern information technology is a latter-day Babel.

Of course, a great many also speak the Windows vernacular, although most are versed only in Windows' point-and-click patois. After some 20-odd years of revisions, the predominance of Windows users have forgotten the arcane DOS dialect.

Indeed, compared to a UNIX shell, the Windows COMMAND utility is woefully underpowered; hence, UNIX users typically find Windows a frustrating platform. Moreover, Windows can be especially vexing for UNIX software developers accustomed to an expansive and rich set of command-line tools. To UNIX developers, using Windows is akin to visiting a strange and foreign land.

Luckily, Cygwin (see Related topics) provides a familiar destination within Windows, like finding a McDonald's in France. (Royale with cheese, anyone?)

Cygwin is a UNIX-like shell environment for Windows. It consists of two components: a UNIX API library to emulate many of the features that the UNIX operating system provides, and adaptations of the Bash shell and a plethora of UNIX utilities to provide the familiar UNIX command-line interface. The former component is provided as a Windows Dynamic Link Library (DLL). The latter component is a collection of individual programs —many built from source code unchanged from UNIX—based on the Cygwin DLL. Combined, the two give you all the comforts of your UNIX home directory, sweet home.

In this month's Speaking UNIX column, you install Cygwin, explore its command-line interface (CLI), and build open source software not included in the standard Cygwin distribution to experience how easy it is to port (at least some) UNIX applications to the emulation environment.

Installing Cygwin

Unlike other software presented here, Cygwin uses the Windows Installer. The Cygwin setup.exe file installs the software anew and is re-used any time you want to add, change, or upgrade components of your Cygwin configuration.

Open a browser on your Windows system, and point to to download the Cygwin installer. The installer itself is very small (about 600KB), because the bulk of the Cygwin software is subsequently downloaded during the setup process. After your download succeeds, perform the following steps to install Cygwin:

  1. Run the Setup program. Figure 1 shows the Cygwin welcome screen.
    Figure 1. The first of many dialog boxes presented during Cygwin setup
    Cygwin welcome screen
    Cygwin welcome screen
  2. Click Next to advance to the next screen and choose the type of installation you want to perform.
  3. Click Install from Internet.
  4. Click Next, then choose an installation directory.

    The window in which you configure your installation is shown in Figure 2.

    Figure 2. Cygwin installation options
    Choose how to install Cygwin
    Choose how to install Cygwin

    In most cases, the recommended installation options are suitable, but you can customize with a few caveats:

    • Do not install Cygwin in the root directory of your Windows system, such as C: It's best to install Cygwin in its own subdirectory, such as the default—C:\cygwin—or perhaps C:\Program Files\cygwin. (The target directory, whatever you choose, becomes the root directory—the /—of your emulated UNIX environment. For example, if you install to C:\cygwin, the virtual /usr/bin can actually be found in C:\cygwin\usr\bin.)
    • Do not choose Just Me for the Install For option.
    • Set the Default Text File type to Unix to maximize compatibility with existing files and files stored on other UNIX machines.
  5. Click Next.

    In the window that appears, choose a directory in which to store the data Cygwin requires. Do not choose the Cygwin directory you selected in the previous step. If possible, create or choose a directory on a drive with at least 1GB of free space.

  6. Click Next again, and choose the type of Internet connection you use. Direct Connection is typically apropos.
  7. Click Next again.

    In a moment, Cygwin downloads its list of current mirror sites and prompts you to choose one, as shown in Figure 3. When in doubt, choose a site within close proximity.

    Figure 3. Choose the installation site you feel is reliable or is in close proximity
    Choose a mirror site
    Choose a mirror site

    After a brief delay, the installer displays a complete list of available categories and packages. Figure 4 shows a partial catalog. Click on a plus sign (+) to expand the corresponding category; click a "loop" to cycle between Skip (which omits the package) and all available versions of the respective package. If Cygwin provides multiple versions of a utility, pick the instance that meets your requirements. By the way, the B column, if selected, downloads the binary package; if the S is selected, the source is downloaded, too.

    Figure 4. Choose the packages and instances that best suit your needs
    Choose your packages
    Choose your packages

    More than 1,000 packages are available within Cygwin, so choose only the categories and packages you need. (Installing all of the Cygwin bundles takes up more than 800MB of disk space.) After all, you can always add an entire category or a single package in the future: Simply re-run the Cygwin installer. (You can also remove or update a package at any time with the same installer.) To quickly determine whether your favorite UNIX utility is available within Cygwin, search the Cygwin package list (see Related topics).

  8. When you've chosen the tools you want, click Next, and the download process begins!

    The status bars reflect each package download, overall download headway, and disk usage, respectively. Figure 5 is a snapshot taken during installation on my test machine.

    Figure 5. Cygwin downloads a great deal of software, so be patient
    A snapshot of download progress
    A snapshot of download progress

    Finally, the Setup application installs the software and (optionally) adds shortcuts to your Start menu and desktop. Click Finish.

  9. Launch Cygwin by using the Start menu or by double-clicking the Cygwin icon (if you installed those shortcuts), or you can execute the Cygwin script found in your Cygwin directory (such as C:\cygwin\Cygwin.bat).

    Figure 6 shows Cygwin as it starts the first time: It creates your home directory, seeds shell startup files, and presents the prompt. You can now run UNIX commands!

    Figure 6. Presto! Now you can use UNIX commands in Windows!
    A Unix shell running in Windows
    A Unix shell running in Windows

    For example, try ls -a or type touch. The latter command indicates that touch is the executable /usr/bin/touch.

The best of UNIX in Windows (and vice versa)

Cygwin is a near-complete emulation of the UNIX shell within Windows. In fact, it marries the two operating systems quite nicely. For example, run the command df -h to display an inventory of free disk space on your "UNIX" machine. Figure 7 shows the result.

Figure 7. Is that a UNIX file system on Windows or a Windows file system on UNIX? Yes!
An inventory of file systems within Cygwin
An inventory of file systems within Cygwin

As mentioned earlier, the Cygwin installation directory acts as the root directory of your virtual UNIX system. Cygwin simply maps subdirectories in its installation directory to familiar UNIX equivalents. It also provides the Windows drives as individual volumes, such as /cygwin/c. You can use such a virtual path to launch Windows programs. Try it: Type /cygwin/c/Program\ Files/Internet\ Explorer/IEXPLORE.EXE to launch Windows Internet Explorer® from the command line. (You can also use the Tab key to automatically expand elements of the path.)

By the way, if you want to convert a Windows path name to its UNIX equivalent or vice versa, try the built-in command cygpath. By default, cygpath produces the UNIX path name. Use the -w option to produce a Windows path.

$ cygpath -w /cygwin/c/Program\ Files/Internet\ Explorer/IEXPLORE.EXE
c:\Program Files\Internet Explorer\IEXPLORE.EXE

Other niceties provide helpful bridges between the two environments:

  • Cygwin provides its own lpr (/usr/bin/lpr rather than Windows' own LPR.EXE) to print from the UNIX emulation directly. Simply set the PRINTER environment variable to a Cygwin UNC such as \\server\printer_name or //server/printer_name—either slash direction works, as in all other parts of Cygwin.
  • Symbolic links created with ln -s in Cygwin are interpreted as shortcuts in Windows. Reflexively, Windows shortcuts are interpreted as symbolic links. Further, you can use a Windows shortcut to launch a UNIX command with parameters.
  • And because you have a full Bash shell at your disposal, you can use all the aforementioned compatibility features (among others) to write UNIX shell scripts to maintain Windows!

    For example, you can use UNIX find to hunt for data on your drives. Here's a tip: it's common to use spaces in a Windows file name. To preserve an entire file name (remember, UNIX arguments are separated by white space), be sure to use find -print0 and its companion, xargs -0.

How to update and extend your Cygwin

As mentioned at the onset, you can add, remove, and update Cygwin categories and packages at any time by (re-)running the Cygwin installer. Let's add a few software development packages to prepare for building additional code from source, as you typically do on a traditional UNIX system.

Run the Cygwin setup.exe application again, and march through the initial dialogs until you return to the Select Packages window, shown in Figure 4. Expand the Devel category, and use the recycle control to select the highest-numbered versions of the following:

  • autoconf2.1
  • automake1.10
  • binutils
  • gcc-core
  • gcc4-core
  • gdb
  • pcre
  • pcre-devel

After making all selections (which may automatically choose additional packages to satisfy dependencies), click Next to start the update. As before, the download and installation process may take some time—the development packages tend to be quite large.

In the meantime, open a browser window and point to to download the latest source code of the GNU wget utility. (If you haven't used it before, wget is a command-line tool to download virtually anything. For more information about this tool, see Related topics) Using Cygwin, copy the file to your home directory in Cygwin, and expand the tarball. Listing 1 shows the contents of the tarball.

Listing 1. Contents of wget-1.11.4.tar
$ cp /cygdrive/c/Documents\ and\ Settings/Martin/My\ Documents/wget-1.11.4.tar ~
$ tar xzvf wget-11.4.tar

When the Cygwin download and installation process finishes, click Finish. You should now be ready to build the wget utility from source.

$ cd wget-1.11.4
$ ./configure
configure: Configuring for GNU wget 1.11.4
creating po/Makefile
$ make
gcc -I. ...
$ make install
/usr/bin/install -c -m 644 ./wget.1 /usr/local/share/man/man1/wget.1
$ type wget

To test the new utility, run it within Cygwin and the download the wget source code:

$ /usr/local/bin/wget

In a moment, you should have a new source tarball. It's easiest to pull utilities from the Cygwin repository using the Cygwin installer, but you can build source you download—or your own source code—within Cygwin. The full suite of UNIX development tools, including the most popular scripting languages, are available within Cygwin.

You can even build native Windows WIN32 applications with the UNIX compilers and tools found in Cygwin if you don't have something like Microsoft Visual Studio®. (These applications will not run on UNIX. To build a Windows application to run on UNIX, consider WINE. See Related topics for more information.)

Familiar, powerful, and useful

Cygwin isn't a perfect emulation of UNIX, but it's a comforting surrogate. The documentation for the core components is good and especially forthcoming, describing limitations in the emulation DLL and security risks. If you want to port a complex UNIX package to Cygwin, consult the Developer's Guide (see Related topics) to measure Cygwin's support for your API needs.

Perhaps the best feature of Cygwin is its integration with Windows. To boost productivity, drop to Cygwin and use its UNIX-like command line to manipulate the system. One suggestion: Extend your shell PATH variable to include subdirectories in Windows' Program Files to launch any binary in your path by simply typing its name.

Better yet, Cygwin can manage multiple jobs in the same window. Press Control-Z to suspend a running job; type bg and fg to run a job in the background and foreground, respectively; and type jobs to manage the list of jobs. Of course, Cygwin also redirects input and output and pipes output from one command to another.

Cygwin: It's better than a Royale with cheese.

Downloadable resources

Related topics


Sign in or register to add and subscribe to comments.

Zone=AIX and UNIX
ArticleTitle=Speaking UNIX: Opening Windows with Cygwin