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
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 Resources) 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.
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 http://cygwin.com/setup.exe 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:
- Run the Setup program. Figure 1 shows the
Cygwin welcome screen.
Figure 1. The first of many dialog boxes presented during Cygwin setup
- Click Next to advance to the next screen and choose the type of installation you want to perform.
- Click Install from Internet.
- 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
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.
- 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
- 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.
- Click Next again, and choose the type of Internet connection you use. Direct Connection is typically apropos.
- 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
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
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 Resources).
- When you've chosen the tools you want, click Next, and the download
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
Finally, the Setup application installs the software and (optionally) adds shortcuts to your Start menu and desktop. Click Finish.
- 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!
For example, try
type touch. The latter command indicates that
touchis 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!
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 -sin 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
findto 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 -print0and its companion,
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:
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 http://ftp.gnu.org/gnu/wget/wget-1.11.4.tar.gz
to download the latest source code of the GNU
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 Resources) 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 wget-1.11.4/ wget-1.11.4/AUTHORS wget-1.11.4/NEWS wget-1.11.4/COPYING wget-1.11.4/configure.bat wget-1.11.4/ChangeLog wget-1.11.4/ChangeLog.README wget-1.11.4/msdos/ wget-1.11.4/msdos/ChangeLog wget-1.11.4/msdos/Makefile.DJ ... wget-1.11.4/src/cookies.c wget-1.11.4/src/http.h wget-1.11.4/src/log.h wget-1.11.4/src/sysdep.h wget-1.11.4/src/alloca.c wget-1.11.4/src/getopt.c wget-1.11.4/src/gnu-md5.h wget-1.11.4/src/ftp.c wget-1.11.4/config.sub wget-1.11.4/config.guess $
When the Cygwin download and installation process finishes, click Finish. You
should now be ready to build the
wget utility from
$ 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 /usr/local/bin/wget
To test the new utility, run it within Cygwin and the download the
$ /usr/local/bin/wget http://ftp.gnu.org/gnu/wget/wget-1.11.4.tar.gz
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 Resources 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 Resources) 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
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.
- Speaking UNIX: Check out other parts in this series.
- Learn more about the features of Cygwin.
- Read the official Cygwin User's Guide.
- Check out the Cygwin package list.
- Explore the Cygwin API Reference to learn more about compatibility with UNIX and how to port to Cygwin.
- Learn more about UNIX shells.
- Learn more about WINE, an open source implementation of the Windows API.
- The AIX and UNIX developerWorks zone provides a wealth of information relating to all aspects of AIX systems administration and expanding your UNIX skills.
- New to AIX and UNIX? Visit the New to AIX and UNIX page to learn more.
- Browse the technology bookstore for books on this and other technical topics.
Get products and technologies
- Check out developerWorks blogs and get involved in the developerWorks community.
- Participate in the AIX and UNIX forums:
Dig deeper into AIX and Unix on developerWorks
Get samples, articles, product docs, and community resources to help build, deploy, and manage your cloud apps.
Experiment with new directions in software development.
Software development in the cloud. Register today to create a project.
Evaluate IBM software and solutions, and transform challenges into opportunities.