Monday, 28 January 2019

What is the Linux Shell

When we use linux we communicate with it to manage our resouces and "do useful things" by means of the shell.  There are a number of different shells (e.g. bash, sh, csh) but they all do the same sorts of things, file management, running applications, etc.  Shell scripts are used to automate a list of actions and make systems easier to use.  I spent a little while investigating what a shell is and found the results interesting.

When you login to linux, the operating system executes a program for you.  You can find out which with :
pi@PI3 - ~ grep pi /etc/passwd
pi:x:1000:1000:,,,:/home/pi:/bin/bash
pi@PI3 - ~

You can change a users shell with:
chsh --shell /home/pi/c/shell.o pi

The program you run is not special, it simply provides a user interface and arranges for commands to be executed, either within the program or by calling the Operating system.  For my experiment I wrote a simple program which displays a command prompt, waits for an input line, echoes the line and repeats.  It terminates when the user types exit.  I set this up as the users shell and logged in.  The program starts automatically. When exit is typed the user connection is terminated.

Stephen Brennan has written a very neat introduction to writing a shell in C which explains how to initiate processes using fork and exec system calls.
A simple introductory fork example is provided at GeeksforGeeks

No comments:

Post a Comment