9

Examples of commands I'm referring to are ls, pwd, and cd. Also, how are these built? Do you have an example?

26

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:

  • OpenSolaris pwd source.
  • FreeBSD ls

You can find many more of these online.

5

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 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.

  • 2
    Well bash, or any other good shell. – rds Aug 16 '11 at 13:19
  • 1
    Note also that some commands such as cd must be a shell builtin to work effectively. – AProgrammer Dec 31 '11 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.