Car981
Car981

Reputation: 261

Why is this error happening in bash?

Using cygwin, I have

currentFold="`dirname $0`"
echo ${currentFold}...

This outputs ...gdrive/c/ instead of /cygdrive/c/...

Why is that ?

Upvotes: 0

Views: 278

Answers (2)

aggregate1166877
aggregate1166877

Reputation: 3150

I would like to add to Gordon Davisson's anwer.

I am also using Cygwin. In my case this happened because my Git for Windows was configured to Checkout Windows-style, commint Unix style line endings.

This is the default option, and was breaking all my cloned shell scripts.

I reran my git setup and changed to Checkout as-is, commit Unix-style line endings which prevented the problem from happening at all.

enter image description here

Upvotes: 0

Gordon Davisson
Gordon Davisson

Reputation: 125798

Your script is stored in DOS format, with a carriage return followed by linefeed (sometimes written "\r\n") at the end of each line; unix uses just a linefeed ("\n") at the end of lines, and so bash is mistaking the carriage return for part of the command. When it sees

currentFold="`dirname $0`"\r

it dutifully sets currentFold to "/cygdrive/c/\r", and when it sees

echo ${currentFold}...\r

it prints "/cygdrive/c/\r...\r". The final carriage return doesn't really matter, but the one in the middle means that the "..." gets printed on top of the "/cy", and you wind up with "...gdrive/c/".

Solution: convert the script to unix format; I believe you'll have the dos2unix command available for this, but you might have to look around for alternatives. In a pinch, you can use

perl -pi -e 's/\r\n?/\n/g' /path/to/script

(see http://www.commandlinefu.com/commands/view/5088/convert-files-from-dos-line-endings-to-unix-line-endings). Then switch to a text editor that saves in unix format rather than DOS.

Upvotes: 2

Related Questions