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 trying to get to grips with Bash scripting via Cygwin. My script is about as simple as it gets.

I change the directory to the root of my C drive, and print the new location.

#!/usr/bin/bash 
cd /cygdrive/c
pwd

is saved in the file chdir.sh in my home directory. I then call ./chdir.sh from the bash prompt. This results in the error

: No such file or directorygdrive/c
/cygdrive/c/Documents and Settings/rcotton

I definitely have a C drive, and the command cd /cygdrive/c works when I call it directly from the bash prompt.

I realise that this problem is likely stupidly simple; please can you tell me what I'm doing wrong.

share|improve this question
add comment

4 Answers

up vote 7 down vote accepted

Just in case you have edited your script with an editor which is not part of the Cygwin environment (e.g., anything like 'Notepad*', 'WordPad', etc.): convert your script to Unix-lineendings via the 'dos2unix' tool.

The script itself is absolutely correct, no need for any / or \ changes. The error message

: No such file or directorygdrive/c

leads me to think of problems with the lineending since \r\n (Windows line ending). Just in case you do not have 'dos2unix' installed:

tr -d \\r < win.txt > unix.txt

or

sed -e 's/$/\r/' < unix.txt > win.txt
share|improve this answer
1  
Very well called. It was sneaky carriage returns causing the weirdness. –  Richard Cotton Mar 22 '10 at 13:11
add comment

Are you sure you're using forward slash, and not backslash, in your cd command? Your problem would be symptomatic of mistaking these two.

Try these variants if just a single forward slash produces is indeed there and produces this weird error:

cd //cygdrive//c

or

cd \\cygdrive\\c
share|improve this answer
    
cd //cygdrive//c gives the error : No such host or network pathrive//c. cd \\cygdrive\\c gives the same error as before. –  Richard Cotton Mar 22 '10 at 12:38
add comment

Try #!/usr/bin/bash.exe and see if that makes a difference. Either works on my Cygwin install, but the file is actually named bash.exe.

share|improve this answer
add comment

TO: ttarchala (and Mr Cotton)

In cygwin, //start1/path_2/stuff/long triggers UNC hack mode, which treats start1 as a SMB server with share path_2. This is not what was intended.

This will sometimes bite you in certain shell scripts that are shared with UNIX, where multiple forward slashes are always coalesced to a single slash. In cygwin, multiple forward slashes are coalesced EXCEPT if it's the beginning one, which is UNC trigger mode.

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.