Bash, Awk on Windows: Git-Bash and ConEmu

Every developer who has worked for some time on Linux or another Un*x system will miss the bash, some of the other unix tools like find, cat, grep and awk and a decent console bitterly.

There are several ways how to get some of the unix power back to windows.

  • Cygwin. It is sometimes a bit complicated to install and handle and there are often problems with line endings. I have used this but never was really totally content.
  • MinGW, MSYS and MSYS2. I have never used them and cannot tell much about them.
  • Git-Bash and ConEmu. Lately I have detetcted these tools and it seems they are working quite well. In this post, I’ll go a bit deeper on them. Git-Bash is a version of mingw32, as far as I know.

Git Bash

Git is a well known source control system. And its installation kit for windows contains bash, awk, grep, find and several other unix tools. So even if you don’t want to use git on your windows machine, you can install this package. It is free software.

Installation (with Git 1.9.5 from 2015/03/19) goes like this:

  1. Download the git package for windows.
  2. Run the installer.
    • Install it to tools/Git. Next.
    • Leave away Windows Explorer integration. Do not associate file endings. Next.
    • Do not create a start menu folder. Next.
    • Use Git from Git Bash only. (Other options didn’t work as intended anyway.)
    • Next. Check out Windows-style, commit Unix-style line endings.
    • Next. Wait. Finish.
  3. Create a home directory for your unix tools where you like it. Create an environment variable called HOME pointing to this directory.
  4. Add to your PATH environment variable the directory C:/tools/Git/bin.
  5. Into the directory pointed to by HOME, put a .bashrc with some aliases.
  6. Make sure that your .bashrc contains this line:
    PATH=/bin:$PATH
    This makes sure that the bash tools find and sort are in the PATH before the windows tools with the same names.
  7. Set the Double Commander terminal command to C:/tools/Git/bin/bash.exe.
  8. Done and works. Now you’ve got a usable bash with pipes and filters, awk, find, grep etc available.

ConEmu

ConEmu is a very fine terminal emulator. Download the ConEmu portable package. It’s got dozens of settings and it can be used for any consoles on windows, not only the bash. E.g. it can be used with the ordinary cmd, too.

Here is how to install it:

  1. Download the portable version of ConEmu.
  2. Extract it beneath the Git directory, to C:/tools/ConEmu.
  3. Run ConEmu.exe or ConEmu64.exe, depending on your system. Delete the other one.
  4. Select as settings location C:/tools/ConEmu/ConEmu.xml
  5. As startup task select {Bash::Git bash}.
  6. Done. Now you’ve got a nice looking console window with your git-bash inside. And for the occasional case you still need a cmd console, you can start this also inside ConEmu.
  7. And the best, you have all these things in the console you’ve missed your whole life:
    • You can copy text to the clipboard by just marking it.
    • You can insert text by typing Ctrl-v. Yes, really. No need to Alt-Space-Edit-Paste.
    • You can resize even the width of the window by dragging at the corner. Shocking.
    • ConEmu remembers its position and size and reopens at the previous place
    • There are dozens of other settings and possibilities for configuration with which you can play if you’d like to.
  8. Set the Double Commander terminal command to:
    C:/tools/ConEmu64.exe -here
>000005

I’ve got the idea of combining ConEmu with Git on Windows from this post.

Things I like to have in my .bashrc:

PATH=/bin:$PATH

alias cd='pushd'
alias -- -='popd'

alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'

alias ls='ls -ACF'
alias ll='ls -l'

alias no='notepad++'

How do You Move Cygwin?

Last week, I had to move Cygwin from my old to my new working PC.

I can tell you, starting new with Cygwin is a completely different thing than copying or moving a working installation to another PC after four years of not updating it. I spent one and a half days on the task and it still does not work exactly like it did before … but I can use it productively. And it was worth the trouble. 😉

What follows is my personal moving cygwin leaflet.

How do I copy or move Cygwin from PC 1 to PC 2?

As said, it was not easy and I had to follw many blind alleys and got lost in the mazes of the Cygwin forums a lot ….. I’m leaving away some of the blind alleys and all of the system reboots in the following description.

  1. Zip the cygwin directory on PC 1. On my PC 1, it is located under C:\cygwin. It contains cygwin’s setup.exe and the packages directory and the bin, usr, etc, lib … directories.  I zipped it to c-cygwin.7z.
  2. Zip the user’s home directory on PC 1. My cygwin username on PC 1 was ‘wic’, and the home was located under D:\wic. I zipped it to d-wic.7z.
  3. Unzip the c-cygwin.7z on PC 2 to C:\cygwin.
  4. Unzip the d-wic.7z, ideally to D:\wic. But my PC 2 has no D:\ drive and I don’t want to repartition it now. So I extract it to C:\wic.
  5. Run c:\cygwin\setup.exe. Select “Install from local directory”. Select “c:\cygwin” as root directory. Select C:\cygwin\packages as local package directory. Click Next, Next, … until setup.exe ends.
  6. Set the Windows environment variable HOME for your user to /cygdrive/c/wic. Literally, as cygwin like path.
  7. Now I can doubleclick my C:\cygwin\cygwin.bat file and it tries to run my olde .profile and so on…..But, I get some strange errors like

    bash: $’\r’: command not found

  8. I’m assuming my .profile has Windows- line-endings but the drive is mounted in binmode. I call ./mount and get

    c: on /cygdrive/c type system(binmode,noumount)
    d: on /cygdrive/d type system (binmode,noumount)

    I call ./od -c ~/.profile  and see a lot of \r\n. So my assumption seems plausible.

  9. I’m trying to remount in textmode: mount -t c:\cygwin /cygdrive/c but I’m getting this error: mount: /cygdrive/c: Device or resource busy.
  10. Somewhere in the net they say if this error occurs, you should unmount first. I’m trying umount -A, and there is no error message. But still, the binmode-mounts stay. I cannot not remount c: as textmode.
    During all the reading in the forums, I find out that the cygwin maintainers changed the treatment of line-endings totally around 2011. I think there’s no way to resist these changes….. After four years, finally I have to update my cygwin.
  11. I download and install the newest version of cygwin. At least the PATH variable is now set correctly, I can call cygwin commands now from every folder as long as I’m inside the bash.
  12. I’m mounting mkdir /c; mount C: /c   Seems to work. Still the mounting of my home directory is wrong.
  13. I’m moving c:\wic to c:\cygwin\home\wic and setting the HOME environment variable to /home/wic. Still, it cannot interpret the windows line endings correctly.
  14. I’m calling d2u (dos2unix) for the most important files:
    cd /c/cygwin/home/wic
    d2u .*   # this works, d2u ignores binary files and directories
    d2u *
    cd bin
    d2u .*
    d2u *
  15. cp /bin/bash.exe /usr/local/bin/bash.exe  # because some of my bash scripts use #!/usr/local/bin/bash
  16. Copy scripts from c-cygwin.7z/usr/local/bin to /usr/local/bin, running d2u * in /usr/local/bin.
  17. When I close the cygwin window and start it again, the mount of c: -> /c is away. Maybe because of lacking rights? I try to start cygwin with admin rights and do the mount again. No, it’s not a rights problem, but mount does only create temporary mounts. If you want to have persistent mounts, you have to change the /etc/fstab file.
  18. Also, I want to replace the /cygwin prefix by /, so that cd c:/ can be written as cd /c.  In the /etc/fstab, replace this line
    none /cygdrive cygdrive binary,posix=0,user 0 0
    this:
    # getting rid of /cygdrive and make all new mounts text mounts.
    none / cygdrive text 0 0
  19. Hooray hooray. Now, I can use all of my scripts, my aliases and all of the un*x commands that come with cygwin. Only some fine tuning is lacking.
  20. Create a shortcut on the screen to Cygwin.bat.
  21. Select the little tree as icon for the shortcut. The tree is my bash symbol for ages. (pic)
  22. Double click it. Right-click on the window header. Select Properties. Choose these properties:
    Quick-Edit-Mode: on
    Insert-Mode: on
    Font:7×12
    Window buffer size: w x h 120 x 3000, window size: 120 x 43
    Window background color: 244,244,188
    Window text color: 0,10,10

cygwin

DONE.

Cygwin: Linux Commands on Windows

Maybe you – like me – have learned to know and love the linux bash with its pipes and filters and its standard commands like sed, grep, find, awk, alias, xargs and so on. Maybe you have to do anything with the Windows batch or Windows command shell now. Then you will not only feel like in the stone age, but you’ll feel personally insulted by how ugly, impractical and unusable the Windows tools are.

02-13 15_44_44-Cygwin Setup

You’ll long for your precious bash and your beloved grep. You are not alone…. That’s why since 1995 people are working on Cygwin. Cygwin is a bash with a huge lot of commands, usable on Windows.   I have been using it now for more than ten years, and there have not been too many working days when I haven’t used it for this or that. I only can recommend it warmly for bash addicts coming to Windows.

To start using it, just download the latest Cygwin installer and run it. You’ll be firing your first find | xargs | grep in a quarter of an hour. It’s that easy.

Though it has some quirks, it can be used quite well as your personal shell on Windows PCs. (I would not recommend to use it on a build server or as part of the software build process. Because of the quirks and because it would be an administration nightmare.)

When it works, it works well. I have not updated one single package of the Cygwin installation on my working PC since 2009…. This week, I had to move the Cygwin to my new working PC. But this is another story.

I’d love to hear from you: Which shell do you use on Windows? Why?

 

What are the quirks, you are asking?

In short: line endings.

Unix line endings are \n, Windows line endings are \r\n. This leads to a whole lot of problems which have never been solved well by the Cygwin developers. And I assume these problems cannot and will not ever been solved well.
Because of the different line endings you have such strange things as being able to mount a directory in textmode or in binmode.

Some Cygwin commands do different stuff, depending on the mount mode of the stuff. When the stuff comes from a binmode mounted directory, it is treated differently than when it comes from a textmode mounted directory.  But not all Cygwin commands do the same. Some treat stuff like this and some like that. And of course, it also depends on the version of the tool. And, as some commands create temp files, your result may also depend on the mount mode of your temp directory.

It may be that cat foo | grep bar  results in different line-endings than grep bar foo.  And that grep bar foo | awk // gives a third type of line-ending. There even have been versions of Cygwin where the commands also differentiated between different path notations.

All of this binmode/textmode/line-endings stuff is just !$%!$%!$.

And you’ll end up with files having \n as line-endings or even \r\n\n or probably anything that matches [\r\n]+ where they should just always have \r\n. This will happen, when you are using Cygwin in a regular way. Mostly, it is just a call of u2d (unix2dos) or some similar command and the file is correct again.