47

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?

8
  • What's your OS? Do you have read permissions on the directory? I'm not sure what to say, as I've never seen this before. Commented Feb 7, 2011 at 5:16
  • where does test reside? will you be running it from the parent directory of test? Commented Feb 7, 2011 at 5:21
  • Ubuntu 10.10, yes I do have permissions in the directory Commented Feb 7, 2011 at 5:22
  • also, process substitution is a bash extension. generally you should use the executing shell /bin/bash when you use special features like $(...) Commented Feb 7, 2011 at 5:27
  • like i mentioned in my answer, check pwd Commented Feb 7, 2011 at 5:29

11 Answers 11

78

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

Sign up to request clarification or add additional context in comments.

3 Comments

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 :)
I god damn love you, dude.
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.
19

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.

2 Comments

Hero, this fixed my issue when cd'ing in a for loop on mac
This fixed my problem. I learned "~" does not resolve to "$HOME" inside of double quotes. Thanks.
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.

1 Comment

Is it possible to do this for absolute paths? e.g cd /dir1/dir2/dir3
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

3 Comments

I am running it from my home directory and yes, I do have a test sub-directory.
Your error message suggests you were running from the root directory '/'. what is the output from pwd when you run it in the shell?
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.
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.

Comments

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.

2 Comments

the current working directory of the execution will be the directory from which the script is launched, not the script's directory
That's what I said, I said it will be base off the directory of where the command was given (launched).
1

Well I got it working using ""

So in your case it would be:

cd "test"

/Marcus

Comments

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.

2 Comments

The working directory of the script will be the directory from which the script is launched. to test, have a script run pwd
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.
0

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

Comments

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"

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.