How to execute a bat file from cygwin bash that uses the Windows find command

The find command does completely different in Windows and Unix. On Windows it is a fgrep-like utility listing matching lines in a file; on Unix -- and on Cygwin -- it list filenames matching some criteria.

Cygwin bash prepends its standard directories to the current path, so inside bash $PATH is typically /bin:/usr/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS.

Begin Update to provide additional details

For example I have many scripts that use the gnu find command -- for example scripts to purge directory trees that contain no files:
find . -depth -type d -empty | xargs rmdir -p

I also have a bat file to start my build, which uses the windows find command, which searches for lines matching a string (similar to gnu grep).

dir | find "target"
if errorlevel = 1 goto no_target_dir

Now, for my bash script to work I need /bin to be in path before c:\windows\system32. But for my bat file to run I need c:\windows\system32 to be in path before /bin

In general we might be able to claim that all bat files should be executed with the original environment inherited by bash, not the modified one. Does that make sense?

End Update

This how it should be, but breaks the bat files executed from bash. What is the best way to address this?

Is there a way to force Cygwin to execute bat files (or even all Windows executables) with the environment it started with? I am thinking of start /i behavior of cmd.exe. I am thinking of writing my own cygstart like utility that does this, by saving the environment (or at least $PATH) in .bash_profile/.bashrc. Does that make sense?

Any other suggestions?

Edit: See also Start new cmd.exe and NOT inherit environment


If you chmod +x your.bat it works.


is run via cmd /c

Already answered in Why is it that Cygwin can run .bat scripts?

I've tested a solution from here and it works nicely:

start "clean shell" /i "%windir%\explorer.exe" "\path\to\your\script.bat"

to-run.bat script:

echo %PATH%
echo "TODO: some commands"

cygwin bash script,

#!/bin/bash -e
set -x

SCRIPT_WINPATH=`cygpath --windows --absolute "$1"`
EXPLORER_CYGPATH=`which explorer`
EXPLORER_WINPATH=`cygpath --windows --absolute "${EXPLORER_CYGPATH}"`

cmd /C start "clean shell" /I "${EXPLORER_WINPATH}" "${SCRIPT_WINPATH}"


chmod +x to-run.bat
chmod +x
./ to-run.bat

You can use a wrapper, say, launcher.bat, whose contents is

@echo off
rem modify here based on your needs
set PATH=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
call %*
exit %errorlevel%

Say you want to call cmdFUBAR.bat with arg wellDoneMS from a cygwin bash script, instead of just cmdFUBAR.bat wellDoneMS, you do cygstart -w launcher.bat wellDoneMS. Of course you have to put launcher.bat into your path, or call it with full path.

=== In the build.bat script...

SET DOS_FIND_EXE=%SYSTEMROOT%\system32\find.exe
dir | "%DOS_FIND_EXE%" "target"
if errorlevel = 1 goto no_target_dir

The answer given by @ruslo is very useful and works just fine, but there a redundancy in it: the start "clean shell" /I does nothing here :-) It is the explorer piece that gets rid of the current cygwin env, not start /i! Try omitting it from the last line of the script, like so:


Same result - "pristine path"!

Perusing the help for start, it says: The new environment will be the original environment passed to the cmd.exe and not the current environment. Well, but the environment passed to cmd in this case is that of cygwin, so start /i will do nothing useful here, as one can see by typing:

cmd /c start /i cmd /c "path && pause"

The path is still that of cygwin, not the windows system!

Need Your Help

Increasing and decreasing the volume of a jukebox(Actionscript 3)

actionscript-3 flash actionscript flash-cs5 volume

So I've been working on this jukebox assignment for some time now. And I've stumbled upon a problem related to changing the volume of the jukebox while playing the animation.

Android : How to open MenuInflater with a button

android button menu imagebutton

I want an option menu on the top right corner in my activity, it should be like a drop down menu if possible, i tried this one, it didn't work for me!!!!