Introduction To Shell Programming

Kernel and Shell

An Operating is made of many components, but its two prime components are –

  • Kernel
  • Shell

  • A Kernel is at the nucleus of a computer.
  • It makes the communication between the hardware and software possible.
  • A shell in a Linux operating system takes input from you in the form of commands, processes it, and then gives an output.
  • It is the interface through which a user works on the programs, commands, and scripts.
  • A shell is accessed by a terminal which runs it.
  • When you run the terminal, the Shell issues a command prompt (usually $),where you can type your input, which is then executed when you hit the Enter key. The output or the result is thereafter displayed on the terminal.
  • While the Kernel is the innermost part of an operating system, a shell is the outermost one.

Types of Shell

There are two main shells in Linux:

  1. Bourne Shell:

The prompt for this shell is $ and its derivatives are listed below:

  • POSIX shell also is known as sh
  • Korn Shell also knew as ksh
  • Bourne Again Shell also knew as bash (most popular)
  • Bourne shell (sh)
  1. C shell:

The prompt for this shell is %, and its subcategories are:

  • C shell also is known as csh
  • Tops C shell also is known as tcsh

Command To View Supported shell types by system

            cat /etc/shells

 

What Is Shell Scripting?

 

  • SHELL SCRIPTING is writing a series of commands for the shell to execute.
  • It can combine lengthy and repetitive sequences of commands into a single and simple script, which can be stored and executed anytime.
  • This reduces the effort required by the end user.

 

steps in creating a Shell Script

  1. Create a file using a vi editor (or any other editor).  Name script file with extension .sh
  2. Start the script with #! /bin/sh
  3. Write some code.
  4. Save the script file as filename.sh
  5. For executing the script type bash filename.sh

 

#! operator

 

“#!” is an operator called shebang which directs the script to the interpreter location. So, if we use”#! /bin/sh” the script gets directed to the bourne-shell.

 

Example:

Let’s create a small script –

#!/bin/sh

ls

 

Adding shell comments

Commenting is important in any program. In Shell programming, the syntax to add a comment is

       #comment

 

Wildcards

There are some characters that are evaluated by the shell in a special way. They are called shell metacharacters or “wildcards.”

These characters are neither numbers nor letters.

For example

the *, ?, and [ ] are used for filename expansion.

The >, >>, and | symbols are used for standard I/O redirection and pipes.

To prevent these characters from being interpreted by the shell they must be quoted.

What are Shell Variables?

Variables store data in the form of characters and numbers. Similarly, Shell variables are used to store information and they can used by the shell only.

For example, the following creates a shell variable and then prints it:

variable =”Hello”

echo $variable

 

Example

#!/bin/sh

echo “what is your name?”

read name

echo “How do you do, $name?”

read remark

echo “I am $remark too!”

Lab Tasks:

  • Write a shell script that prints “Shell Scripting is Fun!” on the screen

 

  • Store the output of the command “hostname” in a variable. Display “This script is running on _.” where “_” is the output of the “hostname” command.

 

  • Write a shell program that adds two numbers.

 

  • Write a shell program that takes 2 integer values from user and then show the sum of values.

Related links

Lab Practice Task

Search within CuiTutorial

Scroll to Top