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?
1 Answer
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.
-
OK. Is there something I should replace
system(lineCopy);
with then?– user102473Jul 26, 2015 at 9:31 -
1You cannot - the child does not change the parent in this respect. Anway why write a shell when not going the hole hog– Ed HealJul 26, 2015 at 9: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
. Jul 26, 2015 at 10:46
cd
needs to be a shell built-in.