Defining aliases in Cygwin under Windows

Here's a really quick and dirty way to do it, but it works fine for most things!

Let's say you want to always run 'ls --color' instead of just 'ls'. Instead of messing around with .bashrc stuff, you can create a simple .bat file that essentially bootlegs the original ls command.

Here's what I did:

cd /bin
echo ls2.exe %* --color > lsNew.bat
mv ls.exe ls2.exe
mv lsNew.bat ls.bat

So now, whenever you type in ls from CMD, you actually are calling ls.bat, which in turn calls ls2.exe --color, the original ls command with the --color flag, along with the rest of the arguments, which are nicely passed through %*.


I had the same issue, where the aliases added to ~/.bashrc didn't work. It seems that, for some reason, the ~/.bashrc was not executed when launching the console.

I stumbled upon a response that fixes the issues

So, you need to create a .bash_profile file. This one seems to be the default script, and put this code in it, to ensure that the .bashrc is executed.

# ~/.bash_profile: executed by bash for login shells.

if [ -e /etc/bash.bashrc ] ; then
 source /etc/bash.bashrc
fi

if [ -e ~/.bashrc ] ; then
 source ~/.bashrc
fi

That works for me, just make sure that .bash_profile is executable. (chmod +x ~/.bash_profile)


As me_and already explained what's going on I just want to add a workaround should you for whatever reason not be able or willing to remove Windows' HOME environment variable.

Normally the shortcut for Cygwin executes

C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico -

Instead you can create a batchfile with the following content and start that:

@echo off
set HOME=
start C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico -

That will start a a Cygwin windows whose home directory settings are not overridden by a Windows environment variable.


Your .bashrc file will be loaded from wherever Cygwin Bash thinks your home directory is when it starts. You've mentioned in your edit that you've changed your home directory, but not how, so it's possible you've made a mistake there.

Cygwin will load your home directory from one of two places, and if they differ it can cause problems:

  • The HOME environment variable. This will be picked up from however you launch Cygwin, so normally from Windows itself. You can see what environment variables you have defined by pressing Win+Pause, going to "Advanced system settings", "Environment Variables…". If "HOME" is in either "User variables" or "System variables", delete it – it's unnecessary and only causes problems.

  • Cygwin's /etc/passwd file (normally C:\Cygwin\etc\passwd from Windows). This will have a number of lines containing details of each user on the system; the seventh : separated field is the home directory. You can tell which user it's looking at by running whoami from a Cygwin bash shell.

If whoami reports nunos, you should have a line in Cygwin's /etc/passwd that looks something like the following:

nunos:unused:1001:513:U-System\nunos:S-1-2-34-567890-123456-7890123-1001:/home/nunos:/bin/bash

It's that /home/nunos that's important; if it's something different you should probably reset it to that, at which point you want to use the .bashrc in Cygwin's /home/nunos/.

You should also be very wary of directories that contain spaces for this. C:\Users\nunos should be fine, but beware in particular C:\Documents and Settings\nunos, which just won't work with Cygwin.

Tags:

Cygwin