Cygwin - cd in bash script

12

3

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.

Dan

Posted 2010-09-16T09:01:43.200

Reputation: 608

1Can you post the actual line(s) from the script? – Paused until further notice. – 2010-09-16T10:37:40.210

Answers

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.

garyjohn

Posted 2010-09-16T09:01:43.200

Reputation: 29 085

Emacs was also saving with CR-LF. Changing the line-ending style in-editor: http://www.emacswiki.org/emacs/EndOfLineTips

– jfklein – 2014-11-27T04:34:41.803

Aha! That was it. I was using Vim to edit it. Thankyou :-) – Dan – 2010-09-16T15:33:10.117

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 – 2010-09-16T18:46:55.970

3

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

CarlF

Posted 2010-09-16T09:01:43.200

Reputation: 8 576

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 – 2016-09-27T07:10:56.617

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 – 2010-09-16T15:30:00.207

2

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

Xenoactive

Posted 2010-09-16T09:01:43.200

Reputation: 992

1

Try:

cd /cygdrive/c/code/blah/blah

Mike Fitzpatrick

Posted 2010-09-16T09:01:43.200

Reputation: 15 062

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 – 2010-09-16T11:39:10.080

This didn't work. – Dan – 2010-09-16T12:08:55.630

@nik the c: form didn't work either. Nothing I try works from a script. They all work from the command line. – Dan – 2010-09-16T13:03:29.780

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

PJ Brunet

Posted 2010-09-16T09:01:43.200

Reputation: 1 044