Operating System - HP-UX
1753788 Members
7754 Online
108799 Solutions
New Discussion юеВ

differnece between "cd" and "/usr/bin/cd"

 
MEYER_8
Occasional Advisor

differnece between "cd" and "/usr/bin/cd"

I'm writing postscript for Ignite and uses the full path for system command, here is my issue:

if I type :
cd /var/tmp ; pwd
The answer is : /var/tmp

If I type :
/usr/bin/cd /var/tmp ; pwd
The answer is : /tmp ( which was my previous working dir)

which cd >> /usr/bin/cd

How do you explaine this ?

8 REPLIES 8
Mark Grant
Honored Contributor

Re: differnece between "cd" and "/usr/bin/cd"

"cd" is a shell internal, i.e the shell handles the changing of the directory itself. the default action of the shell internal version od "cd" is to say where you are, logically. In other words if you "cd " to a symbolic link, "cd" will report the symbolic link name, not the actual directory. You can change this by going "cd -P".

"/usr/bin/cd" is a binary that changes your working directory. It's default behavior is to show you the real directory you are in.

My guess is that you have /var/tmp symbolically linked to /tmp
Never preceed any demonstration with anything more predictive than "watch this"
Alan Turner
Regular Advisor

Re: differnece between "cd" and "/usr/bin/cd"

Since the shell runs commands in a sub process, running /usr/bin/cd changes the current directory of the subprocess, but not of the main process (the shell process). Therefore use "cd" (the shell built-in) rather than /usr/bin/cd.

It's hard think of any use for /usr/bin/cd, but try "man cd" if you're interested - on 11i, it gives an example of the use of "cd" in a find command which might equate to /usr/bin/cd.
Mark Grant
Honored Contributor

Re: differnece between "cd" and "/usr/bin/cd"

actually, Alan's answer makes much more sense :)
Never preceed any demonstration with anything more predictive than "watch this"
Volker Borowski
Honored Contributor

Re: differnece between "cd" and "/usr/bin/cd"

Hi,

as said "cd" is internal to the shell and therefor does not deliver an exit code.

In case you create a script and want to validate, if a "cd target_dir" will succeed, you can use the binary in an if-statement and you will have existence and permission-check all in one.

Bad script:
cd /tmp/mydir
rm *

Now just assume /tmp/mydir is not accessible for whatever reason...... Directory where you have been before will be destroyed.

Better script:
if /usr/bin/cd /tmp/mydir
then
do what you like here
else
do what you not like here
fi


I have to say I never really used that, but I remember a school-lesson that said it should be used that way.

May be you can let us know if you tried it out ?
Volker
MEYER_8
Occasional Advisor

Re: differnece between "cd" and "/usr/bin/cd"

I'm satisfy with the answers.
Using cd in my scripts works.

Is there a list of the internal commands ?
Jeroen Peereboom
Honored Contributor

Re: differnece between "cd" and "/usr/bin/cd"

Check the man-page from the shell?!

JP.
Jdamian
Respected Contributor

Re: differnece between "cd" and "/usr/bin/cd"

Hi Volker
You typed:

'as said "cd" is internal to the shell and therefor does not deliver an exit code.'

That is false.

Supose /kkad doesn't exist:

# cd /kafd; echo "return code is $?"
/usr/bin/sh: /kafd: not found
return code is 1
Volker Borowski
Honored Contributor

Re: differnece between "cd" and "/usr/bin/cd"

Hi J. Damian,

thank you for showing this.
In fact I took this from "man cd"
:
Another usage of cd as a stand-alone command is to obtain the exit status of the command.
:
And assumed (wrongly) that the internal one does not deliver an exit code.
May be this manpage-info is outdated and it was so in earlier days.

So the only difference is, that the executable creates a real new subprocess, thus making this processing diffrent:

# cd /
# if cd /tmp; then echo I could [and did] change to /tmp; else echo Did not work; fi
I could [and did] change to /tmp
# pwd
/tmp

# cd /
# if /usr/bin/cd /tmp; then echo I could [and did not] change to /tmp; else echo Did not work; fi
I could [and did not] change to /tmp
# pwd
/
#

Confusing :-)
Volker