Question

I have a shell script that runs like this on my Ubuntu 12.04 LTS server:

cd /var/www/srv

But, for some reason, when I run it, it says: ./start.sh: 1: cd: can't cd to /var/www/srv

The directory exists, and it is run as root so no question of privileges. To add to the peculiarity, when I run the code in the terminal, it works.

Was it helpful?

Solution

This is a classic carriage return issue, caused by creating a shell script in a Windows/DOS editor.

Your problem:

$ cat start.sh
cd /

$ ./start.sh
cd: 1: can't cd to /

Your diagnosis:

$ cat -v start.sh
cd /^M

$ shellcheck start.sh
In start.sh line 1:
cd /
    ^-- SC1017: Literal carriage return. Run script through tr -d '\r' .

Your fix:

$ tr -d '\r' < start.sh  > fixed.sh
$ chmod +x fixed.sh
$ ./fixed.sh
(no errors)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top