Bash & Windows

As far as I'm concerned, the death of DOS batch files has been greatly exaggerated.  Running a batch file is still the only way to perform a series of actions without the help of either third-party software or a macro-running behemoth like Word, Excel or the Windows Scripting language. Fortunately, the GNU development tools - including bash - have been ported to MS-DOS and Windows systems. This page shows you how to hook bash into the Windows operating system. So you'll be familiar with bash once Linux breaks through ;-)

Freddy Vulto

Page Updates

Aug 28, 2001:   Added Cygwin configuration.
Aug 17, 2001:   Upgraded to DJGPP bash v2.04. Added DJGPP versus Cygwin comparison. Added NedStat counter.
Feb 26, 2001:   Updated personal FAQ.
Feb 18, 2001:   Usage of HTML::EmbPerl & HTML::Toc to generate this page.
Nov 26, 2000:   Added Vim paragraph.  Updated bash configuration after downloading bash 2.03. Modified paragraph layout.
Jul 18, 2000:   Changed paragraph 'DOS-box & Start;Run' to use 'App Paths' registry key. Changed DJGPP location in examples to D:\Programs\DjGpp.
Mar 25, 2000:   Moved page location.  Updated 'Shell to DOS' for Windows 98.  Added shell script 'Create .zip file'.
Apr 18, 1999:   Added section 'Shell Scripts'.
Apr 17, 1999:   Modified layout.  Updated section 'Shell Script Automation'.

Table of Contents

1  Where To Get Bash For Windows?

Bash, short for GNU Bourne-Again SHell, is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash is a part of the GNU software collection, utilizing this collection to reach its full potential. The GNU software is an essential part of the Linux operating system, but is also available on the Windows operating system as it's being ported by either Cygwin or DJGPP.

Both the DJGPP and Cygwin project are offering a complete 32-bit C/C++ development system, based on the GNU development tools. The projects differ in the way they handle the connection with the operating system and in their licensing. DGJPP compiles the applications as DPMI (32-bit DOS) applications which can't access the Win32 API. Software created with DJGPP's libc is licensed under GNU Lesser GPL. Cygwin compiles applications as Win32 applications, utilizing a DLL which acts as a UNIX emulation layer. Software created with Cygwin's libc must be distributed as Free Software.

Choose a distribution, or install both. I suggest Cygwin because its Win32 API access allows you to run even more useful GNU tools - like wget and lynx - from within a DOS box. See Cygwin as the successor of DJGPP.

1.1  Cygwin

The URI:

will contain a link to a downloadable setup.exe which will guide you through the process of downloading and installing Cygwin. Take care when installing everything: I had to clean up my harddisk to make room for the 250 MB.

1.2  DJGPP

The URI:

will contain all the links to download sites.  You can download sources (these files end with 's'), binaries (these files end with 'b') as well as documentation (these files end with 'd').  I haven't done any compilation myself and just downloaded:

which is basically bash.  To make full use of the bash shell scripting language, I also downloaded:

The version numbers of these zip files may have changed over time: just download the files with the highest number.

2  Configuring Bash

My current bash configurations can be downloaded here:

2.1  Cygwin

The bash shell runs on top of command.com. In order to meet bash' rather large demand of environment space, it's often necessary to enlarge this space by specifying /e:<number> to command.com. Although this can be done by putting SHELL= in config.sys, I dislike messing with autoexec.bat and config.sys, so I'm starting bash from a preliminary bath file cygwin.bat which enlarges environment space. After the environment space is enlarged, a second batch file _cygwin.bat is called:

2.1.1  cygwin.bat

cygwin.bat contains the following commands:

The option /e:1024 gives me 4 times as much environment space as standard command.com which has the environment space set to 256 bytes.  If you ever receive the error "Out of environment space", you're free to increase this number.  The option /k _cygwin.bat tells command.com to start the batch file _djgpp.bat immediately after command.com is initialized.  The arguments %1, %2, etc. will pass optional arguments (e.g. shell scripts) to bash.

2.1.2  _cygwin.bat

2.1.3  /etc/profile

The file /etc/profile contains global - for all users - bash initialization commands. Make sure to remove the cd $HOME command, otherwise the Shell to Cygwin won't go to the current directory. My /etc/profile currently looks like this:

2.1.4  ~/.bashrc

My .bashrc for Cygwin bash currently looks like this:

2.2  DJGPP

The bash shell runs on top of command.com. In order to meet bash' rather large demand of environment space, it is necessary to enlarge this space by specifying /e:<number> to command.com.  Although this can be done by putting SHELL= in config.sys, I dislike messing with autoexec.bat and config.sys, so I'm starting bash from a preliminary batch file djgpp.bat which enlarges environment space.  After the environment space is enlarged, a second batch file _djgpp.bat is called. This file sets a few environment variables - necessary for bash to start - and finally calls bash:

2.2.1  djgpp.bat

Bash.bat contains the following commands:

The option /e:1024 gives me 4 times as much environment space as standard command.com which has the environment space set to 256 bytes.  If you ever receive the error "Out of environment space", you're free to increase this number.  The option /k _djgpp.bat tells command.com to start the batch file _djgpp.bat immediately after command.com is initialized.  The arguments %1, %2, etc. will pass optional arguments (e.g. shell scripts) to bash.

2.2.2  _djgpp.bat

The file _djgpp.bat sets the environment settings, necessary for bash to start, and finally starts bash:

2.2.3  ~/_bashrc

The file _bashrc may contain commands which are executed by bash on startup. Mine looks currently like this:

3  Configuring Windows

3.1  Start;Run

In order to be able to type either 'cygwin' or 'djgpp' from the Start;Run window, modify the registry like this:

3.1.1  Cygwin

3.1.2  DJGPP

3.2  DOS-box

In order to be able to run 'bash' from the DOS-box, add a batch file to the properties of the DOS-box via {Properties, tab 'Program', Batch file}, and let this batch file add the bash directory to the path:

3.3  Folder Popup Menu

It would be nice if we could jumpstart bash from a specific folder name by selecting so from the folder's right-mousebutton-menu.  Fortunately, Windows 9x offers us the capabilities.  Add the following keys to the registry.

3.3.1  Shell to Cygwin

3.3.2  Shell to DJGPP

3.3.3  Shell to DOS

And while your at it:

3.4  Shell Script Automation

It would be nice if shell scripts could be run by doubleclicking on them or by specifying them via {Start; Run}.  Also it would be convenient if shell scripts could be editted by selecting so from the popup menu.  These are the necessary registry items to accomplish all this, choose either Cygwin or DJGPP:

3.4.1  Cygwin

3.4.2  DJGPP

4  Configuring Applications

4.1  Microsoft Developer Studio

The C++ IDE of Microsoft's Developer Studio offers you the ability to execute 'Custom Build', 'Pre-link step' or 'Post-build step' commands.  These can be bash commands as well.  To show the standard output of bash in the 'Output' window of DevStudio, you have to redirect normal output to stderr, using redirection symbol '>&2'.  For example, suppose you want to execute the script C:\hello.sh as a Post-build step:

Start Developer Studio, select {Project;Settings}, go to the rightmost tab 'Post-build step' and type as 'Post-build command(s)':

4.2  Vim (gVim)

To set bash as the default shell of your GUI Vim, put the commands underneath in your _gvimrc file. Now you can type :sh[ell] to invoke bash, or :r !ls to insert the contents of the current directory, or :make to make your project.

4.2.1  Cygwin

4.2.2  DJGPP

5  Shell Script Examples

5.1  Create .zip file

The following shell script creates a .zip file using the PKZIP command line utility, version 2.50, from PKWARE Inc.

5.2  Detecting Operating System

(credits: Pontus Goffe)
This shell script determines the current operating system (Windows 95 or Windows NT) bash is running from:

6  Personal FAQ

6.1  Create .tar.gz file

To create a tar archive file, compressed with gzip, I use the following command:

or for short:

6.2  Add installed package to Info

After installing a new package x, use the following command to add the info files of the x package to the info main menu:

6.3  Tilde expansion

The following test didn't work:

because for tilde expansion the string has to be unquoted: or even better, as the $HOME directory was what I wanted: See chapter 'Tilde expansion' in bash documentation for more information.

6.4  /usr/bin/pod2man: not found

Using the Pod::Usage function pod2usage() gave me the error:

This happened after I'd put CYGWIN=ntea into my .bashrc for testing purposes. Removing CYGWIN=ntea from my .bashrc made pod2man work all right again.




Home