I am trying to create an alias to use in my Cygwin shell to determine if the Mercurial-managed directory I'm currently in has outstanding changes.  The command hg st -m lists all modified files.  Unfortunately it doesn't set an exit value based on whether there are changes or not.  So I have tried capturing the output and seeing if it's not empty, piping it through wc -l and checking if that's -gt 0, but I seem to have a larger problem in that lines are clearly not executing in the order I would expect them to.Here is what I've boiled it down to:alias hgfoo='pwdecho L1localChanges=NOT SET YEThasLocal=NOT SET YETecho Before: A${localChanges}Z A${hasLocal}ZlocalChanges=`hg st -m`echo L2echo localChanges: A${localChanges}Zecho L3if [ -n ${localChanges} ] ; then    echo L3    hasLocal=YESelse    echo L4    hasLocal=NOfiecho L5echo RESULTS: A${hasLocal}Zecho L6'I put in the Ln echo statements for debugging.  When I run it by typing in hgfoo in a directory that has changes, I see the following output:/d/hg/succession > hgfoo/d/hg/successionL1Before: ANOT SET YETZ ANOT SET YETZL2localChanges: AL2localChanges: ANOT SET YETZL3L3L5RESULTS: AYESZL6M succession-lib\.actionScriptPropertiesM succession-lib\src\main\flex\com\workscape\SuccessionConstants.asM succession-lib\src\main\flex\com\workscape\succession\nomination\tabmodules\BenchStrengthTab.mxmlM succession-lib\src\main\flex\com\workscape\succession\nomination\tabmodules\BenchStrengthTabMediator.asM succession-lib\src\main\flex\com\workscape\succession\puremvc\controller\StartupSuccession.asZL3L3L5RESULTS: AYESZL6When I run it in a directory without changes, I see:/d/hg/employee-profile > hgfoo/d/hg/employee-profileL1Before: ANOT SET YETZ ANOT SET YETZL2localChanges: AL2localChanges: ANOT SET YETZL3L3L5RESULTS: AYESZL6ZL3L3L5RESULTS: AYESZL6Even forgetting about the fact that the comparison is not working right, in both cases it seems to be running lines multiple times and in the wrong place.  This is driving me crazy.Any clues?  Thanks.
Alias in cygwin bash doesn't seem to run in the right order
bash;cygwin
This is a long-standing bug in bash's alias expansion. I could reproduce it on Debian lenny amd64 with bash 3.2.29(1), Debian squeeze i386 with 4.1.5(1), and Windows XP with Cygwin 1.7.7-1 with bash 4.1.9(3).A few experiments show that the bug is very sensitive to variations in how the alias is used. hgfoo or hgfoo : exhibits the bug, but { hgfoo :; } and (hgfoo) work fine. Using a function instead of an alias also works.I investigated a bit, trying to extract a smaller example. The weirdness seems to stem from having a newline in the alias. Here is a simple example where bash (3.2.29 or 4.1.9) misbehaves. This alias works as expected (from my reading of Single Unix v3) under pdksh, ksh93, ash and zsh under Debian lenny, namely oops prints foo bar.$ alias oops='a=$(echo bar)> echo foo $a'$ oopsfoo foo echo barThe bug may be related to a reported bug about strange delayed aliases, but that discussion focuses on line continuations (backslash-newline) which is not used here.In any case, use a function! Aliases are for one-liners where you're just specifying some default options for a command. A function would have the additional benefit that you could pass arguments to hg:hgfoo () {  localChanges=$(hg st -m $@)  } I initially thought this was a Cygwin bug, but as ak2 pointed out, this bug occurs under Linux as well.