Search code examples
macoscmdpromptcd

CD command not working as supposed to


Look at the following code:

ECHO A2
MKDIR -v  /tmp/"My Batch Script File Assignment"
ECHO

ECHO A3
CD "/tmp/My Batch Script File Assignment"
PWD
ECHO

can anyone tell me why the cd command under ECHO A3 isn't working. The following PWD command reports that the working directory changes to /tmp. But I am wanting to changing to the "My Batch Script File Assignment". Any one know why it is not dong this.


Solution

  • I'm assuming you are using bash on OS X.

    Unlike the other commands in your "script", the change directory (cd) command must be in lower case because it needs to be the shell builtin cd, not /usr/bin/cd (whose purpose escapes me).

    The other commands "work" using all caps because paths and file names on OS X are case insensitive (by default anyway), so the shell finds the normal lower case commands on your PATH and invokes them. That doesn't work for CD. You need to use cd.

    So:

    MKDIR invokes /bin/mkdir (ok)
    ECHO invokes /bin/echo (ok)
    PWD invokes /bin/pwd (ok)
    CD invokes /usr/bin/cd (not ok, need to use shell builtin cd)