Linux SHELL Scripting Tutorial 

What is Linux Shell

What is shell scripting?

Shell Script Consist of set of commands to perform a task.
All the commands execute sequentially
Some task like file manipulation, program execution,user interaction, automation of task etc can be done

Types of Shell

  • Bourne shell
  • C shell
  • Bourne Agin shell
  • Korn shell
  • TCsh Shell

Bourne Shell

Bourne shell is known as the first shell to be introduced, it is represented by “sh”. This shell got popular because of its quite compact nature. It was made the default shell for the SOLARIS operating system and was used as a Solaris administration script. It has very high-speed operations. 

C shell

The C shell was designed by Bill Joy at the University of California. It is represented using “csh”. The C shell was designed with the purpose of supporting programming languages. It was specifically designed to support in-built features like solving arithmetic operations and syntax of programming languages like C. Unlike Bourne and other Linux shells, the C shell can maintain and history of previously used commands, and those commands can be used whenever required. 

Bourne Agin shell

It is also known as Bash Shell, This shell combines features of the Korn shell and C shell. This shell was designed as an extended version of the Bourne shell. Bourne Again Shell can automatically load previously used commands and can be edited with the help of the arrow keys of the keyboard. 

Korn shell

This shell was developed by David Korn in AT & T bells lab, this was introduced as an improved version or superset of the Bourne shell. It is represented by “ksh”. It has all the features and functionalities of Bourne Shell and also provides some new functionalities to the users. Korn shell has in-built support for arithmetic operations. 

how to write shell script in linux

Shell Scripts are written using text editors. On your Linux system, open a text editor program, open a new file to begin typing a shell script or shell programming, then give the shell permission to execute your shell script and put your script at the location from where the shell can find it.

  1. Choose a Text Editor:
    Use a text editor to create and edit your shell script. Popular text editors include nano, vim, emacs, or even graphical editors like gedit or VSCode.
  2. Open a Terminal: Open a terminal on your Linux system. This is where you will write and execute your shell script.

Create a New File:
Use the text editor to create a new file with a .sh extension. For example, you can name your script

Copy code

Create a New File:

Use the text editor to create a new file with a .sh extension. For example, you can name your script
Copy code
Add Shebang:

The shebang (#!/bin/bash) at the beginning of your script tells the system which shell to use to interpret the script. Bash is one of the most common shells.
Copy code

Write Script Commands:

Add the commands you want to run in your shell script. For example:
Copy code:
echo "Hello, World!"
ls -l
Save and Exit:

Save the changes to your script and exit the text editor.
In nano, press Ctrl + O to save and Ctrl + X to exit.
In vim, press Esc, then type :wq and press Enter to save and exit.
In other editors, follow their respective save and exit commands.
Make Script Executable:

You need to give your script executable permissions. In the terminal, navigate to the directory containing your script and use the chmod command:
Copy code
chmod +x

Run the Script: Execute your script by typing its name preceded by ./. For example:

bashCopy code./
What are Shell Variables?

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

Naming Conventions:
Variable names are case-sensitive and consist of letters (uppercase and lowercase), numbers, and underscores.
By convention, variable names are usually written in uppercase to distinguish them from regular shell commands and make them more visible.
You can assign a value to a variable using the = operator, without spaces around it.
There should be no spaces between the variable name, the equal sign, and the value.
Example: my_var="Hello, World!"
Accessing Variables:

To access the value stored in a variable, you use the variable name prefixed with a dollar sign ($). For example, to print the value of my_var, you would use echo $my_var.

When using variables in commands, it's a good practice to wrap the variable name in double quotes. This helps prevent issues with special characters or spaces in the variable value.
Example: echo "$my_var"
Predefined Variables:

The shell also has a set of predefined variables that provide information about the environment and system settings. Examples include:
$USER: Current user's username.
$HOME: Home directory path.
$PWD: Current working directory path.
$PATH: Search path for executable files.
And many more.
Using Variables in Commands:

Variables can be used within commands and strings. For example:
Copy code
echo "Hello, $name!"
Arithmetic Operations:

Shell variables can hold numeric values and be used for arithmetic operations.
Copy code
sum=$((num1 + num2))
echo "Sum: $sum"

Environment Variables:
Environment variables are special variables that affect the behavior of the shell and user environment. They are usually in uppercase letters.
Examples: PATH, LANG, SHELL, TERM, etc.

Remember that variables are local to the shell session where they are defined. If you want a variable to be available across different shell sessions or scripts, you might need to use environment variables or save the variables to a configuration file

  • Kernel is the nucleus of the operating systems, and it communicates between hardware and software
  • Shell is a program which interprets user commands through CLI like Terminal
  • The Bourne shell and the C shell are the most used shells in Linux
  • Linux Shell scripting is writing a series of command for the shell to execute
  • Shell variables store the value of a string or a number for the shell to read
  • Shell scripting in Linux can help you create complex programs containing conditional statements, loops, and functions
  • Basic Shell Scripting Commands in Linux: cat, more, less, head, tail, mkdir, cp, mv, rm, touch, grep, sort, wc, cut and, more.
  • Linux SHELL Scripting Tutorial 

4 thoughts on “Linux SHELL Scripting Tutorial ”

Leave a Comment