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?
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?
I had this problem, and was very confused for a while.
It turns out I had set my
$CDPATH
environment variable, which normally allows regularcd
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: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 (usingsh
), it failed withI modified it to be a relative path:
and it works! I believe the difference is in how the shell uses
CDPATH
. In one case, it searches bothCDPATH
and your current directory, but in the script it only searchesCDPATH
. This is similar to the behavior ofPATH
. If you leave.
(the current directory) out of yourPATH
, then you have to type./localbinary
instead of justlocalbinary
to execute that file.This is my educated guess. When I set / unset
CDPATH
it breaks / unbreaks thecd subdir
command, andcd ./subdir
works in all cases for me.2
is the errno for "No such file or directory". Are you sure the scripttest
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.Well I got it working using ""
So in your case it would be:
/Marcus
I had the same problem. Turned out the problem was \r\n line endings.
To fix it, do
From http://talk.maemo.org/showthread.php?s=1cadd53b369d5408c2b9d53580a32dc4&t=67836&page=2
put
pwd
as the first line. Then see if that directory has a test subdirectory.It looks like its running from the root directory