Examples of commands I'm referring to are ls
, pwd
, and cd
. Also, how are these built? Do you have an example?
-
3Is this homework? – Stefan Lasiewski Jul 26 '11 at 17:59
2 Answers
It's usually plain C
. The commands ls
and pwd
come from the GNU Coreutils
package in (most?) Linux distributions (and maybe some other systems). You can find the code on their homepage.
For coreutils
specifically, you build them with the usual steps: after unpacking the source, issue:
./configure --prefix=/some/path
# type ./configure --help to get the available options
make
make install # could require root access depending on the path you used
Be carefull - installing base utilities like those over your distribution's copy of them is a bad idea. Use whatever package manager your system comes with for that. You can install to a different prefix though (installing somewhere into your home directory is a good idea if you want to experiment).
Note that although there is a cd
executable, the cd
you'll be using in most circumstances isn't a separate executable. It has to be a shell build-in (otherwise it could not change the shell's current directory - this has to be done by the process itself), so it is written in the same language as the shell (which is often C
too).
Other examples:
You can find many more of these online.
Just to add a bit more to Mat's answer, although Unix implements these as standalone programs, many commands are also implemented within bash
(to save it having to spawn a new process) see the bash
man page for more details, or have a look at the shell-builtin tag.
bash
is also written in C
It may be worth noting that these commands do not have to be written in C - indeed, several mini linux distribution use lua
implementations of these and others.

- 28,907

- 5,540
-
2
-
1Note also that some commands such as
cd
must be a shell builtin to work effectively. – AProgrammer Dec 31 '11 at 15:20 -
You should give an example of a program that's also been implemented in bash and show how. – Michael Moreno Mar 17 '22 at 10:45