Thursday, May 14, 2009

Bash environment on Windows: This is not Cygwin

Bookmark and Share

If you are a Linux/UNIX guy and are forced to use Windows then you may find this post useful. Linux/Unix guys use windows due to many reasons. One of the reasons is Microsoft Outlook. This is must have for me and there is no application that can take its place in Enterprise environment.

Today, we will try to setup bash environment of Windows XP. If command prompt is launched then it user should able to change his/her default shell to bash shell and should able to execute generally used Unix tools.

There are few software packages that need to be downloaded.

1. Download bash-203.zip and extract it to “c:\bash”

2. Now download Native Win32 ports of some GNU utilities to “c:\temp\UnxUtils”

Now copy all files from “c:\temp\UnxUtils\usr\local\wbin” to “c:\bash”. It should look like this,

BASH directory on windows

Now add “c:\bash” to global PATH environment variable as below,

PATH Environment variable on Windows for bash

On windows, we can also add “HOME” environment variable which will be passed to bash shell.

Once PATH variable includes “c:\bash”, launch Program=>Accessories=>Command Prompt and type in “bash”.

Bash environment is ready on windows. All bash shortcuts will also work as equally as on Linux.

I also know that there could be other ways to get this work done on windows. If you use this or similar technique then let me know your experience in comments below.


9 comments:

  1. Very useful for the simplicity of the approach and the handy links. I've used bash that comes with MsysGit and Cygwin - both very large distributions.

    Sean

    Read: http://www.openmakesoftware.com/blogs/seanblanton
    Follow: http://www.twitter.com/seanblanton
    Connect: http://www.linkedin.com/in/seanblanton

    ReplyDelete
  2. I just followed these instructions - I'm quite happy to have some additional unix utils on my command line (cat, less, rm) - but I could not get bash itself to work.

    It will run fine, but reports "bash: warning: could not find /tmp, please create!" - I tried creating a "tmp" within C:\Users\myusername (where I set my HOME environment to), and also within C:\bash, but this did not stop the error.

    Not so bad though, right? Just a warning. Well, actually it's worse: immediately after opening bash, a command like "ls" will display files in the good old fashioned style, then dump this to me: "[sig] C:\bash\bash.exe 1000 (0) call_handler: couldn't get context of main thread, error 998", and hang. After a CTRL+C, you end up with "[sig] bash 1000 (28) call_handler: couldn't get context of main thread, error 998", and now the terminal is entirely nonresponsive.

    I'm running 64-bit Win7 Ultimate on an Intel Core2, about 4gb of ram and the rest is unimportant. I'm guessing this is a 64-bit issue or just a "Win7 is different" issue, which is causing some DLL call to fail or return differently and muck things up. If I find a fix I'll report back; I'm also cross-posting this to my own blog.

    Thanks for the suggested method to bring bash to Windows!

    Wyatt

    ReplyDelete
  3. Hi Wyatt, I ran into the same issue with bash. However, I've downloaded another window bash program (http://win-bash.sourceforge.net)and fortunately it works fine. And there is no dll to worry about - it's completely self-contained.

    Mike

    ReplyDelete
  4. the tmp directory should be created in the root of the system:
    mkdir /tmp. This worked for me.
    Setting Home environment variable got rid of complaint about .bashrc

    ReplyDelete
  5. the same problem with 'tmp'. I tryed to create this folder inside:
    %BASH HOME DIR%
    c:\Windows
    c:\Windows\system
    c:\Windows\system32

    it wont works.

    I installed win-bash (bash_nt) and its working now.

    ReplyDelete
  6. For those of you who have run into the problem with the tmp file, while in bash, in looks for tmp at the root. So in bash,

    mkdir /tmp

    or go to the C:\ and right click, New, Folder, and name it tmp.

    As for the home directory, right click on "My Computer", and go to Properties, Advanced, and at the bottom, click "Environment Variables." On the system variables at the bottom, select Path and add: ;c:\bash at the end of the line. Then on the "User Variables" at the top, click "New" then give the name Home and the value C:\Documents and Settings\username

    This should do it.

    HTH,

    KaBar's Edge
    Registered Linux User #431029
    Windows free since 05/2000

    ReplyDelete
  7. I'm (being forced to) use window 7. I could not create /tmp using bash so I did it using windows and it did not complain.
    For almost every command I get
    [sig] C:\bash\bash-2.03\bash.exe 1000 (0) call_handler: couldn't get context of
    main thread, error 998

    But this warrants further investigation time and effort. I will watch this space. Let me know if I can help.

    Cheers
    Tony

    ReplyDelete
  8. Hi everyone..
    I am trying to use bash-2.03 and its giving me:

    bash: //.bashrc: Invalid argument
    bash-2.03$
    what should I do????

    ReplyDelete
  9. Hello. Quite an old thread, but bash under Windows is always useful ^^ So, here an info that explains :

    Like you (everybody), I've downloaded the current bash-203.zip from here and expanded it under "c:/bash". Then, I've added the HOME environment variable as "%HOMEDRIVE%%HOMEPATH%" (this way, it works with all Windows user accounts).

    And, effectively, when I run "bash" from Windows prompt (I'm under Seven Pro 64-bit), I fall in the warning "could not find /tmp".

    From this point, I've add the idea to add the TMPDIR env. variable (Windows using TMP and TEMP rather), but it doesn't help (same warning)... And I wondered if bash.exe could take its temporary directory from another env. var...

    Thus, I've (quickly) investigated the thing, this way : just downloaded the most recent source code of original bash (4.2) from http://www.gnu.org/software/bash/ and looked inside :

    In "tmpfile.c", we just see the responsible function called "get_sys_tmpdir()"*, and this function doesn't rely on any environment variable but try several hardcoded paths (what a bad way), including "/tmp".

    So, the only way (unless modifying this function ; for example going through the other one called "get_tmpdir()" that can interrogate the TMPDIR environment variable) is to physically create "c:/tmp" (or one of the others paths indicated in bash's source code, like "/var/tmp" or "/usr/tmp") :o(

    However, a simple bash without Cygwin is really nice : yeah :)

    Have a good day

    Eric
    ffh-lab.com

    --
    * Here is the function :

    static char* get_sys_tmpdir()
    {
    if (sys_tmpdir)
    return sys_tmpdir;

    #ifdef P_tmpdir
    sys_tmpdir = P_tmpdir;
    if (file_iswdir (sys_tmpdir))
    return sys_tmpdir;
    #endif

    sys_tmpdir = "/tmp";
    if (file_iswdir (sys_tmpdir))
    return sys_tmpdir;

    sys_tmpdir = "/var/tmp";
    if (file_iswdir (sys_tmpdir))
    return sys_tmpdir;

    sys_tmpdir = "/usr/tmp";
    if (file_iswdir (sys_tmpdir))
    return sys_tmpdir;

    sys_tmpdir = DEFAULT_TMPDIR;

    return sys_tmpdir;
    }

    ReplyDelete

Your valuable comments are welcome. (Comments will be moderated.)

 




Technology