Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
1  
Can you post the actual line(s) from the script? –  Dennis Williamson Sep 16 '10 at 10:37
add comment

5 Answers

up vote 11 down vote accepted

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.

share|improve this answer
    
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
add comment

Try:

cd /cygdrive/c/code/blah/blah

share|improve this answer
    
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
add comment

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
share|improve this answer
    
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
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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