Adding Linux to Windows Using Cygwin

There are several options for adding the power of Linux to Windows but one of the best is Cygwin. It offers all the really good stuff, it's well maintained and it's free. You can create a real Linux environment and have a good implementation of the bash shell and the most convenient system level commands.
The first thing you'll need is the Cygwin setup.exe file from Cygwin. Once it's downloaded run the setup.exe file. It will ask some questions and then present a list of things to download and install. The most important thing is to select a place to put it. I suggest a separate partition from the Windows partition (Drive C) since Cygwin is a fairly large and complex install (see more on partitioning here).

The setup will open a window with a long list of things available to install. Some of these items are fluff of course so go through the list and select stuff you really want and ignore the rest. What's neat about the setup.exe install method is that you can re-run it whenever you want and add or remove the odds and ends from the previous install. This helps you to configure Cygwin any way you want as often as you want.

Once the initial install completes go to the cygwin directory. You'll see a cygwin.bat file that has the startup commands. Since it starts from Windows as a batch file it uses batchfle style commands:

@echo off
echo Welcome to Bill's Computer *** All Accesses are Logged ***
set CYGWIN=tty notitle glob
/cygwin/bin/bash -l
The actual command to start cygwin is the line, bash -l. We're telling cygwin to execute bash as a login shell rather than simply running a command. This gives you the option of configuring you own environment. Here an excerpt from the bash manual entry:
"When bash is invoked as an interactive login shell, or as a non-interac tive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable."
What this means is that you can create a .bash_profile file in your HOME directory that contains commands you want to run every time you login (I'm assuming that you are somewhat familiar with the way Linux does things but if you're not, check here for some help). Here's a sample .bash_profile file:
export PS1='${PWD#*}> '
export HOME=/home/acme
export TERM=ansi
alias spell='aspell check'
alias web='cd /cygdrive/d/Apache2.2/htdocs'
alias webpages='cd /cygdrive/e/webserver/webpages/Bill'
alias c='cd /cygdrive/c'
alias d='cd /cygdrive/d'
alias e='cd /cygdrive/e'
alias find='find /cygdrive/e/cygwin '
The PS1 variable is the prompt set to display only the current directory, a space and a > sign. You can set this to just about anything but the version here is the one I always use. The export directive tells the shell that the variable is to be available in all subsequent sub-shells. The alias commands just substitute an existing command with one you create, like a macro. Notice that cygwin starts it's path with the root drive (partition where it's installed) flowed by the directory where it's installed. In the cygwin install directory is a sub-directory named, cygdrive. In this directory are sub-directories to every partition on the disk (assuming there are any). To have cygwin locate a file or directory, create an alias pointing to the cygwin path.

From this point on you'll have a Linux environment every time to run the \cygwin\cygwin.bat file. But wait! You can do more. By adding the cygwin system directories to the Windows PATH variable, you can have all the Linux commands you know and love available from even the Windows command prompt. Go to Start -> Settings -> Control Panel -> System -> Advanced system settings -> Environment Variables and look for PATH. Select Path and then edit and add the cygwin executables paths (this is all one line):

C:\Program Files%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;e:\cygwin\bin;e:\cygwin\sbin;e:\cygwin\usr\x11r6\bin;
Notice two things here: it's a real pain to set the Windows variables because of all the menus you have to navigate and, secondly, you can modify the PATH to arrange executables in whatever order you want. If you put the cygwin paths at the beginning of PATH variable, they will execute before a Windows command of the same name.

After all this you can now access the cygwin commands from a Windows command prompt or, optionally, execute the cygwin.bat batch file to login into the full cygwin environment. But you can do even more! Add Xwindows and you can run an xterm window with all the handy stuff xterm provides (copy-paste being what I use most). Cygwin has an x-server you can run from \cygwin\usr\x11r6\bin\startxwin.bat or, if in the cygwin environment (logged into cygwin), /cygwin/usr/x11r6/bin/ and you'll have an xwindow server running.

Edit the startxwin file and modify the line that starts xterm with whatever option you like (this is all one line):

xterm -geometry 91x50+180+30 -e /bin/bash -l
I had some trouble with the cygwin x-server so I switched to Xming And it works with no problems. I also generally use at least two xterms so I can copy and past between them.

Once you've configured your new Cygwin (Linux) environment, you can access all the commands and utilities it offers and create your own shell scripts to manage things much more efficiently (and a lot faster) in Windows. What's especially cool is that you now have all the Linux stuff available from the Windows command prompt (if you modified the Windows PATH variable) so you can run Linux commands as easily as Windows commands.

You can re-run the Cygwin setup.exe file any time to add or remove features so, if you miss something on the initial install, re-run setup.exe and select that feature. Cygwin will download and install it. Since most configuration files are plain text, you can easily modify a program by editing its configuration file. Cygwin even includes a good implementation of the vi editor (see this) for this kind of thing. For those of us who prefer a Linux environment but have to work in Windows, Cygwin is an excellent choice that provides all the good stuff.

Return to Contents Page