Cygwin – cd in bash script

bashcygwin;

I'm new to using Cygwin, so apologies if this is a very newbie question.

I have a bash script where I "cd" to a directory then do something in that directory. However, the cd command fails with "No such file or directory /c/code/blah/blah".

If I copy the cd line directly into the shell prompt, then it works fine. It just fails in the script.

[edit] As requested, I've added the actual lines from the script:

#!/bin/bash
cd /c/Code/Project

Thankyou for any help with this,
Dan.

Best Answer

What kind of line endings does your script have? For Cygwin bash script to work properly (without having to set special options), it must have Unix line endings (LF) rather than DOS line endings (CR-LF). If you saved the script with DOS line endings, bash will see your argument to cd as /c/Code/Project^M, where ^M is a CR, and won't find a directory by that name.

To see which kind of line endings it has, you can execute file scriptname, where scriptname is the name of your script. To convert the script so that it has Unix line endings, execute d2u scriptname.

Don't use Notepad to edit Cygwin bash scripts. It always saves files with DOS line endings.

Related Question