12

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.

  • 1
    Can you post the actual line(s) from the script? – Dennis Williamson Sep 16 '10 at 10:37
18

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.

  • Aha! That was it. I was using Vim to edit it. Thankyou :-) – Dan Sep 16 '10 at 15:33
  • Vim will work fine. It automatically detects whether files have Unix or DOS line endings and will save them the same way. By default, Cygwin's vim will create new files with Unix line endings, but Windows' gvim will create new file with DOS line endings. You can change that. See :help ff and :help ffs. – garyjohn Sep 16 '10 at 18:46
  • Emacs was also saving with CR-LF. Changing the line-ending style in-editor: emacswiki.org/emacs/EndOfLineTips – jfklein Nov 27 '14 at 4:34
2

Unless you override, a script executes in its own copy of the shell (usually Bash). Then when the script exits, that instance of bash also exits. So your script CDs into a new directory and then exits, returning you to the original Bash--which never did a CD.

Two ways to work around it. You can use an alias instead of a script, e.g. in your .profile have

alias mycd="cd /c/Code/Project"

Another way is to tell Bash not to spawn a subshell by using the "dot" syntax

. myscript
  • You've misread my question. The error message I asked about is written to the stdout by the cd command, which is within the script. I'm not asking for the current directory to have been changed once the script exits. – Dan Sep 16 '10 at 15:30
  • I had a similar problem to the OP but without error ; this answer helped me a lot, even if it is not "100% on-topic". – Frosty Z Sep 27 '16 at 7:10
2

You might find your files in /cygdrive/c...

1

Try:

cd /cygdrive/c/code/blah/blah

  • That will work for 'cd c:/code/blah/blah' not for 'cd /c/code/blah/blah'. Also, the 'c:' form works from a script too; Something else is missing here, the question needs more elaboration. – nik Sep 16 '10 at 11:39
  • This didn't work. – Dan Sep 16 '10 at 12:08
  • @nik the c: form didn't work either. Nothing I try works from a script. They all work from the command line. – Dan Sep 16 '10 at 13:03
1

I don't have reputation to comment or vote but CarlF's answer helped me.

"So your script CDs into a new directory and then exits, returning you to the original Bash--which never did a CD."

Same script, but in my case, no error message.

This worked for me:

. myscript.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.