Implementation of a Linux Shell

The shell is used to write commands and execute them. But the question ,”How does a Linux shell work?” is quite relevant. Before continuing our discussion, one thing you must understand that a shell is also a process or a task(as called in the Linux world).

The working of a basic shell can be explained as follows
1. Wait for user input (command)
2. Parse the input to get the command and the arguments
3. Create a new process to execute the command (Use fork() system call)
4. In the new process, execute the command (Use execve() system call) and exit with the status of the command executed
5. In the parent process, wait for the new process to terminate(Use wait() system call). Get the exit status of the child process (User may need it!!)
6. Goto step 1 to continue waiting for the user input.

A simple C program can be used to demonstrate this

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define MAX_LEN 	1024    /* Maximum Length of the command alogwith the arguments */

#define ARG_LEN 	128     /* Maximum Length of Argument */

#define ARG_COUNT	64    	/* Maximum number of arguments */

int main(int argc, char *argv[], char *envp[])



         char 	*arg[ARG_COUNT];

         char 	cmd[MAX_LEN];

         char 	*pos = &cmd[0];

         int 	cmd_len, pid, status;

  	 printf ("\n$ "); 		/* Displaying the prompt */

 	 fgets (cmd, MAX_LEN, stdin); 	/* Reading user input	 */

         cmd_len = strnlen(cmd, MAX_LEN);

 	 int i=0,k;


              int 	len;

              char 	a[ARG_LEN];

              sscanf(pos,"%s",a); 		/* Parsing out the arguments */


	                     arg[i] = malloc(len);

              if (!arg[i]) {

                    printf("Unable to allocate memory\n");




	      pos+=(len+1);		/* Repositioning: for next argument */

	}while(pos<cmd+cmd_len);   	/* Obtained the arguments */

      	pid=fork();			/* Create child process to execute command */

	if(pid==0){       		/* In child process */

		arg[i] = NULL;

        	if (execve(arg[0], arg, envp) == -1){

              		 perror ("Error");


         	exit(1); 	 	/* Reached if Child failed to execute */


	wait(&status);  		/* Wait for Child to terminate */

/* continue */


return 0;



# /bin/ls /

bin   dev  home  lib64       media  mnt    net  proc  sbin       srv  tmp  var

boot  etc  lib   lost+found  misc   opt    root  selinux    sys  usr

If you run this program, you will see $ in the beginning of the line. But the shell we have designed is not comparable to the bash shell or any other shell. You can not use the arrow keys for navigation. To execute some simple commands like ls, you have to specify the complete path (/bin/ls). But these are the features that we have to add.

So any shell utilizes the above basic program adding on more and more features!!

Comment if you found any bug and like to add some features to the basic shell



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s