python -c 'import os; os.chdir("/")'
Notice that the working directory of your shell is unaffected. :-)
Or:
bash -c 'export foo=bar'; echo $foo
That being said, aside from a kernel patch there is definitely no official way of doing it. This is the hacky way:
http://stackoverflow.com/questions/2375003/how-do-i-set-the-...
Sample:
/tmp$ exec /tmp/cd /bin
/bin$ exec /tmp/cd /home
/home$ exec /tmp/cd /usr
/usr$
Sample code: #include <err.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char *shell;
if (argc != 2)
errx(1, "Usage: exec cd /some/path");
if (chdir(argv[1]) == -1)
err(1, "chdir");
shell = getenv("SHELL");
if (!shell || !*shell)
shell = "/bin/sh";
execl(shell, shell, NULL);
err(1, "exec");
}This seems a little awkward and I'm not sure what is being proven. :P
When they introduced something like the fork/exec model we now have they discovered cd didn't work any more and had to write it as a built in.
Now, for bonus points, work out how goto worked when it wasn't a built in.
Makes sense, since those systems might not have enough memory to run the shell and another program at the same time.