46

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?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Abhishek
  • 6,862
  • 22
  • 62
  • 79

11 Answers11

76

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

Benito Ciaro
  • 1,718
  • 12
  • 25
  • 1
    Excellent. I had this issue when running Docker on Windows host and mounting windows folder as a directory in linux container. Adding this comment in hopes it will get indexed by search engines for people like me :) – Mchl Jan 11 '17 at 15:25
  • I god damn love you, dude. – Jim Chen May 26 '19 at 15:57
  • Logged into SO just to upvote this! Thanks. – rekle Mar 24 '22 at 20:28
  • Same problem with WSL / docker. You could also open the file from Windows in Notepad++, and then do Edit > EOL Conversion > Unix (LF), then save the file. – Timmos Jul 04 '23 at 08:58
17

Not really relevant for this question. I had the same error message, however, I was using

cd ~/foo/bar

After changing this to

cd $HOME/foo/bar

it was fixed.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
5

I had this problem, and was very confused for a while.

It turns out I had set my $CDPATH environment variable, which normally allows regular cd commands to work as usual. However, I was running my script in non-interactive mode, as "sh" (not "bash"), where the behavior is a little different. It seems that a command like:

cd subdir  # works via interactive bash; not in script run via sh.

will work as expected in my interactive login shell, bash, even when CDPATH is set. However, when I run the identical command in a script (using sh), it failed with

myscript.sh: line 9: cd: subdir: No such file or directory

I modified it to be a relative path:

cd ./subdir

and it works! I believe the difference is in how the shell uses CDPATH. In one case, it searches both CDPATH and your current directory, but in the script it only searches CDPATH. This is similar to the behavior of PATH. If you leave . (the current directory) out of your PATH, then you have to type ./localbinary instead of just localbinary to execute that file.

This is my educated guess. When I set / unset CDPATH it breaks / unbreaks the cd subdir command, and cd ./subdir works in all cases for me.

Tyler
  • 28,498
  • 11
  • 90
  • 106
4

put pwd as the first line. Then see if that directory has a test subdirectory.

It looks like its running from the root directory

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • I am running it from my home directory and yes, I do have a test sub-directory. – Abhishek Feb 07 '11 at 05:21
  • Your error message suggests you were running from the root directory '/'. what is the output from `pwd` when you run it in the shell? – Foo Bah Feb 07 '11 at 05:25
  • I cna't `cd` to the output of `pwd` just shows "no such file or directory". But if I copy/paste the output to a `cd` in the terminal it works just fine. – Douglas Gaskell Apr 04 '23 at 21:09
2

The answer by Benito Ciaro is on point. I would just like to add another method that you can use to remove \r\n line endings. Open the script in text-editor Sublime and in the menu

Goto View → Line Endings → Unix

This will remove the '\r' character from your script. Don't forget to save your file.

1

Well I got it working using ""

So in your case it would be:

cd "test"

/Marcus

Oldek
  • 2,679
  • 5
  • 23
  • 25
1

It depends on where the script is being executed from, if the script is in your $PATH, then it will be based off of the current directory you gave the command from (working directory).

If this is a script being run as a cron job, it's best to use a full directory path.
Example:
cd /home/user/test

Giving the full path will also work if the script is in your $PATH.

KazW
  • 21
  • 2
  • the current working directory of the execution will be the directory from which the script is launched, not the script's directory – Foo Bah Feb 07 '11 at 05:22
  • That's what I said, I said it will be base off the directory of where the command was given (launched). – KazW Feb 07 '11 at 05:25
0

2 is the errno for "No such file or directory". Are you sure the script test exists in the working directory of the script?

You might want to cd to a known "good" directory first and then cd into known child directories of that good directory.

Matthew Iselin
  • 10,400
  • 4
  • 51
  • 62
  • The working directory of the script will be the directory from which the script is launched. to test, have a script run `pwd` – Foo Bah Feb 07 '11 at 05:23
  • And the working directory of the script is where you'll start from when you `cd` to a directory, is it not? So if you run the script from a directory that does *not* have a `test` directory, it will fail. One solution here is to `cd` with the absolute path of `test`, or its parent (depending on the remainder of the script), allowing the script to be run from anywhere. Which is what I have already mostly stated in this answer. – Matthew Iselin Feb 07 '11 at 05:28
0

I faced the same problem in ubuntu. My command shell is:

$ export DIR=mydir

then execute a script file that contains:

#!/bin/sh
cd ~/$DIR

giving output:

cd: 2: can't cd to ~/mydir

by trying many options, at the end it can only be solved like this:

#!/bin/sh
WORKDIR=~/$DIR
cd "$WORKDIR"
eQ19
  • 9,880
  • 3
  • 65
  • 77
0

I don't know much about this but I changed the permission of the folder and it worked for me

chmod -R 775 ubuntu
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Prarthana
  • 11
  • 2
-1

Make sure you are in the right directory

Run the command bellow to known where are you

pwd

Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The cd succeeds, but as soon as the subshell exits, you're back in the interactive shell and nothing ever changed there.

Try this

. myscript.sh
Garf365
  • 3,619
  • 5
  • 29
  • 41
Zepp
  • 48
  • 4