I have written a very basic shell in C. It reads the input, then runs it using system(lineCopy);
(lineCopy being a copy of the input). All commands will run fine in it, other than cd
. When I try to cd
to a different directory, it simply stays in the current directory. If I try to cd
to a directory that doesn't exist, it says, as it should sh: 1: cd: can't cd to /some_nonexistant_directory
. When I try to cd to a directory that DOES exist, however, it reads that command, but then stays in the same directory. The variable in which I store the current PWD is continuesly updating, so it cannot be that. All other commands work perfectly. But why does cd
not?
Asked
Active
Viewed 885 times
0
1 Answers
0
Try changing an environment variable - that will not work either. System inherits the environment and the current working directory. So each system call will inherit the current working directory from its parent.

Ed Heal
- 258
- 2
- 11
-
-
1You cannot - the child does not change the parent in this respect. Anway why write a shell when not going the hole hog – Ed Heal Jul 26 '15 at 09:34
-
… or try defining an alias (and using it on the next line). … or getting a
history
listing. … or putting a command into the background with&
and then looking at it withjobs
or waiting for it withwait
. … or settingumask
. … or typingexit
orlogout
. – G-Man Says 'Reinstate Monica' Jul 26 '15 at 10:46
cd
needs to be a shell built-in. – Michael Homer Jul 26 '15 at 08:23