sniper sniper - 1 year ago 99
Linux Question

How to implement a new shell command?

I want to implement my own version of ps command & I am trying to understand how shell commands are implemented in Linux. Is it part of the Shell application or part of the module?

In my understanding, when I type ps in shell prompt: the actual implementation for ps can only reside in kernel & shell just invokes a kernel api/binary.

Now, if I want to add a new command (say myps): what should I modify in shell application? & What should I modify in kernel module?

How shell application knows the list of commands supported in individual modules (kernel, network, fs, etc) ?

Lastly, if (just for example) network module is configured & built only for ipv4 and then there is no point in supporting ipv6 commands in shell? How is this taken care?

Answer Source

ps is just a regular program (typically located at /bin/ps). You can find its location on your system by running which ps.

When you run a command (assuming it's not a builtin function, alias, etc. implemented by the shell itself), the shell searches the directories listed in your PATH environment variable. If you're using sh or bash as your shell, you can see it by running echo $PATH. The shell searches the directories in the order they're listed in PATH, and runs the first matching program it finds.

If you want to create a new version of ps, just write a program and put it in one of the directories on your PATH. Typically somewhere like /usr/local/bin/ (accessible by all users) or ~/bin (in your home directory). Or you could add a new directory to your PATH. No need to mess with the kernel or the shell itself, thankfully.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download