Is the cd cmd part of the kernel?

Hi All

I'm looking at writing something that handles changing directory in the calling shell and wanted to take a look at the cd command.

which cd

comes back with nothing and

man cd

does nothing either.

Where is the cd command?

I can see why it wouldn't be with the rest of the commands in /bin as it wouldn't change the directory in the calling shell if it was run in a sub-shell like the other commands are.

Is it part of the kernel?

If so would it be possible to write a kernel module to do what I want? I've been looking at tutorials on kernel programming but haven't found anything that tells me if I can extend the basic Unix command set this way. Most of the examples seem to be driver oriented or basic hello world examples writing to the logs.

Any help appreciated

Cheers

It's neither a program, nor a part of the kernel, but a shell builtin. Same as echo (for most shells), eval , and sometimes even kill and printf .

If you want to enhance it, you can always write a shell function which supersedes the original command.

The cd command is implemented by invoking the chdir() system call which is indeed part of the kernel.

Hi Pludi

Thanks. I've already written this (PushPop) in Korn shell and it works very well but is of course now dependent on the user being a Korn shell user. I now want to implement the project independently of the user's login shell.

Hi Perderabo

Thanks that's really helpful.

Could you elaborate a little more? As a Korn shell user, when I type cd is the Korn shell using a built in to call chdir()? So every shell such as bash and C etc has its own wrapper called cd?

Every shell has a cd command. They all invoke chdir() to accomplish it.

1 Like

OK thanks :slight_smile:

Hi,

'cd' command is not part of the kernel. though it is the part of the shell. Its a shell built-in. The other commands are external to the shell like 'file', 'find' . If a command is shell built-in the shell can invoke it directly . But if its outside the shell, the shell normally creates another process. As pointed above, almost all the commands (userspace) utilities like shell, daemon processes, commands (file, find, etc) uses system calls which in turn is in kernel space.

Thanks and Regards,
Gaurav.