"cd" does not work in shell script

Abhishek picture Abhishek · Feb 7, 2011 · Viewed 46.4k times · Source

I am wondering why cd does not work in shell script. It is as follows,

#!/bin/sh
cd test
mkdir $(date +%d-%mm-%Y)

When I run this, I get can't cd to test

cd: 2: can't cd to /test

Why is it like this?

Answer

Benito Ciaro picture Benito Ciaro · Jul 11, 2012

I had the same problem. Turned out the problem was \r\n line endings.

To fix it, do

tr -d "\r" < oldname.sh > newname.sh

From http://talk.maemo.org/showthread.php?s=1cadd53b369d5408c2b9d53580a32dc4&t=67836&page=2