CODError
CODError

Reputation: 779

Shell commands are written in what language?

There are many shell commands, like

ls, cd, cat etc.

what programming language is used in writing these commands? How are they compiled?

My understanding:

Shell is a program which takes command; ** does this mean that it interprets those commands(like ls is interpreted by shell program)?**

One more question, what language is Shell program written in?

Upvotes: 15

Views: 9519

Answers (3)

user1937198
user1937198

Reputation: 5348

The programs are ordinary executable written in any language (mostly C).

The shell takes a command entered which is just a string. It then looks for certain sequences of characters which have special meaning to the shell such as environmental variables which are $ followed by a word or redirects which are > followed by a path. After this substitution has been preformed it has a string which is split on spaces to generate a name of an executable and parameters. The shell will then search for the executable in the list of directory's in the environmental variable PATH. The shell then uses system calls to create a process from the executable with the parameters.

For example to execute the command ls $HOME the shell would first recognize that $HOME is an environmental variable and substitute it for its value in this case /home/user leaving the command ls /home/user. It then splits the command to on the space to get the executable name ls and parameter /home/user. The shell finds the first executable that matches ls usually /bin/ls. It then uses ether the spawn()/ posix_spawn() or fork() and exec() system calls to create the new process.

Upvotes: 5

vinay hunachyal
vinay hunachyal

Reputation: 3891

Most of the basic utilities in linux are written in C .This u can verify in busybox source code which supports most of basic linux command utility which are written in C. So command like ls,cd ...etc are in c

How shell will interpret check in below link

in an operating system there is a special program called the shell. The shell accepts human readable commands and translates them into something the kernel can read and process.

http://www.math.iitb.ac.in/resources/manuals/Unix_Unleashed/Vol_1/ch08.htm

Upvotes: 11

Paul Evans
Paul Evans

Reputation: 27577

These programs are mainly written in the C programming language as is the linux kernel.

Upvotes: 7

Related Questions