Tips for Launching CygWin

This is part three in a series of tips on using CygWin, a Linux emulator for Windows that provides the best of both worlds. (Parts one and two gave an overview of CygWin along with tips for installing it.) Now, we’ll get into the various files and folders that are created by the CygWin installer; what they mean and how to use them to launch CygWin and begin to take advantage of CygWin — and, as always, tips, tips, and more tips.

The files and folders that are created by the CygWin installer:
The first time the CygWin installer runs, it creates all of the following subfolders within the CygWin installation folder. (Hereafter, we’ll assume the installation folder is C:\sys\cygwin.) If you are familiar with Linux at all, you’ll undoubtedly recognize these names.

bin
etc
lib
tmp
usr
var

A seventh folder, called “home”, is also created automatically, but not until the first time CygWin is launched. (More on that in a moment.)

The installer also generates a Windows batch file that can be used to launch CygWin. This file is called “cygwin.bat,” and is placed directly in the CygWin installation folder (C:\sys\cygwin). Typically, there is no reason to change this batch file, but if it is changed, it will be left alone by subsequent runs of the installer (which is actually done often — see part two of this series).

Batch File Details:
Tip #1: The default batch file works great. It does not need to be modified at all. However, knowing what the batch file looks like, and understanding how it works can demystify much magic:

@echo off
C:
chdir C:syscygwinbin
bash --login -i

The “echo off” in-line 1 tells Windows not to display the following lines of the batch file as they’re executed. (The leading at-sign tells Windows not to display the “echo off” line itself.) Line 2 sets the current drive to C, just in case it is not already. Line 3 sets the current directory to the bin folder. (bin is short for binary, a standard folder name in Linux for a folder that contains executables. In the case of CygWin, of course, these are Windows executables that emulate Linux executables.) Line 4 actually launches CygWin (specifically, the CygWin version of the “bash” shell interpreter). The “–login” command line switch tells bash to initialize the session according to how the current user is logged in under Windows (every user can customize his/her “profile”). The “-i” command line switch tells bash to run interactively (i.e. by opening a command prompt window).

Tip #2: For detailed information on the command line switches available for launching bash, start CygWin (i.e. a bash session) and then give the command “man bash” in order to view the manual pages for bash itself.

Home Folders: The home folder in CygWin works just like the “Documents and Settings” folder in Windows. For each user, there is a subfolder in home that corresponds to their login ID (e.g. C:\sys\cygwin\home\Craig Jones). The user’s home folder is automatically set as the current directory when CygWin launches.

Tip #3: Linux uses the tilde character (~) as a shortcut for the user’s home folder. So, giving the command “cd ~” is the equivalent of giving the command “cd /home/Craig Jones” (assuming that the logged-in user is Craig Jones, of course). Being able to refer to the home folder as just “~” makes it easy to write shell scripts that use the home folder without having to know the actual name of the folder.
First time CygWin login
The user’s home subfolder is created automatically the first time it’s needed. When it is created, it is automatically populated with three script files: “.bash_profile”, “.bashrc”, and “.inputrc”. These three files collectively provide the profile information for the user, and the user is allowed to change them. It is these three files that are executed automatically when the –login command line switch is specified.

Tip #4: The .bashrc file contains a section entitled “History Options,” about halfway down, line 60 or so. By “history”, this is referring to the fact that Bash remembers all of the commands that are entered, so that they can be recalled using the up and down arrow keys. This makes it simple to re-execute a command that was previously executed without having to retype it all. There are three options listed in this section, and for some reason they are all disabled by default, but the first two options (HISTCONTROL and HISTIGNORE) help immensely. So, uncomment those two lines (remove the leading “#”).

Drive Letter References: As far as CygWin is concerned, the operative “root” folder is the CygWin installation folder (C:\sys\cygwin), and all folder path references are relative to the root. Obviously, though, there are times when you will want to refer to files and folders that are outside of the CygWin installation folder, either elsewhere on the same drive, or on another drive. CygWin allows for this by placing a special object, called cygdrive, in the root folder. (In the CygWin world, cygdrive is the equivalent of “My Computer” in the Windows world.) So, cygdrive is a virtual file folder that acts as the parent folder for the drives that are connected to the computer. And, each drive itself is represented as a virtual subfolder of cygdrive. So, for example, if the computer has a C: drive and a D: drive, and both drives have a folder called “junk” (i.e. C:\junk and D:\junk), then the CygWin way of referring to those junk folders would be /cygwin/c/junk and /cygwin/d/junk.

Tip #5: CygWin, being a blending of Windows and Linux, is somewhat schizophrenic when it comes to file, folder, and drive letter references. File paths are always displayed the Linux way, but CygWin understands file paths that are entered in either the Linux way or the Windows way. For example, the command “mkdir /cygdrive/c/junk” is exactly the same as the command “mkdir C:\junk”.

Tip #6: Notice that the Linux way always uses forward slashes (/), whereas the Windows way always uses backslashes (\). Do not try to mix and match styles. A path reference must be entirely the Windows way, or entirely the Linux way.

Your First Commands: You have already seen examples of the cd command (to change the current directory), and the mkdir command (to make a new directory). Some other commands that will immediately be of interest are:

  • help - displays rudimentary help information about the built-in commands.
  • man - displays the manual pages that correspond to a command, if any.
  • ls - lists the contents of the current directory. (Use the -l command line switch for a “long listing” that provides details rather than just the names.)
  • more - displays the contents of a file one screen full of the time (with a prompt to get more, hence the name).
  • source - run a script. (There will be more on this in and upcoming tip article.)
  • logout - ends the bash session and closes the window.
  • exit - same as logout.

Comments

  1. Hi, I am using cygwin. I am a beginner and does not know how to go about to run a script. If I use run , then I get an error which says that it could not start.

  2. Hi!

    My .bashrc is not being executed. If I get on a DOS cmd and type

    bash -i

    It executes it.

    bash –login

    does not. I don’t get it.

Post a Comment


Your email is never published nor shared. Required fields are marked *



© 2006-2007 Maxim Software Corp.  All rights reserved.