Basics of Shell Scripting

Unix is a multi-user operating system that can handle numerous users’ activities at the same time. The development of Unix was near 1969 at AT&T Bell Labs by Ken Thompson and Dennis Ritchie.

Kernel − Kernel is regarded as the operating system’s heart. It interacts with the hardware and tasks like memory management, task scheduling, and file management.

Shell − The shell processes your requests. When we write a command in the terminal, the shell interprets the command and calls the program that you demand.

Users communicate with the kernel through a program called as the shell. A command line interpreter is what the shell is.

A shell script is a computer program designed to be run by the Unix/Linux shell which could be one among the following:

  1. The Bourne Shell
  2. The C Shell
  3. The Korn Shell
  4. The GNU Bourne-Again Shell

current date and time −


Assume we create a script. It’s important to keep in mind that all the scripts would have the .sh extension. The shebang construct is used to notify the system that a shell script is being run. As the example given below −


Shell Comments

# This will not be excecuted


The three basic types of files are as follows :−

  1. Ordinary Files − Its the file on the system that contains data, text, or program instructions.
  2. Directories − Directories store special and ordinary files both.
  3. Special Files − These files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other kind of special files are similar to aliases or shortcuts and enable us to access a single file using different names.

Listing of files

$ ls

ls command supports the -l option. This option will help you to get more information about the listed files

$ ls -l
  1. First Column − It represents the file type and the permission given on the file.
  2. Second Column − It indicates how many memory blocks the file or directory uses.
  3. Third Column − It represents the owner of the file. This is the name of the Unix user who made this file.
  4. Fourth Column − It represents the group of the owner. Every Unix user will be assigned to a group.
  5. Fifth Column − It represents the file size in bytes.
  6. Sixth Column − It represents the date and the time when this file was created or modified for the last time.
  7. Seventh Column − It represents the file or the directory name.

Above Prefix and its explanation are as follows

It is a Regular file, such as an ASCII text file, binary executable, or hard link.
It is a Block special file. Block input or output device files such as a physical hard drive.
It is a Character special file. Determines raw input or output device file like a physical hard drive.
It is a Directory file that contains a listing of other files and directories.
It is a Symbolic link file. Links on any regular file.
It is Named pipe. A mechanism for interprocess communications.
It is a Socket that is used for interprocess communication.


Metacharacters have a special meaning in Unix. * and ? are metacharacters. We use ” * “ to match zero or more characters and a question mark (?) with a single character.

$ ls log*.txt

Renaming Files

Use the mv command to alter a file’s name. The following is the syntax:

$ mv old_file new_file

Deleting Files

To delete an existing file, we use the command “rm“. The following is the syntax:

$ rm filename

To remove multiple files simultaneously we use the following command.

$ rm filename1 filename2 filename3

Creating Directories

To create Directories The following is the syntax:

$mkdir dirname

Removing Directories

Directories can be deleted using the rmdir The following is the syntax:

$rmdir dirname

We can remove multiple directories at a time tThe following is the syntax:

$rmdir dirname1 dirname2 dirname3

Changing Directories

We can use the cd command to do more than just change to a home directory. We can use it to change to any directory by specifying a valid absolute/relative path. The following is the syntax:

$cd dirname

Renaming Directories

The mv (move) command is also used to rename a directory. The following is the syntax:

$mv olddir newdir

Running The Script

The shell is, after all, a real programming language, complete with variables, control structures, it is just a list of commands executed sequentially. The script below uses the read command which accepts the input from the keyboard and assigns its value to variable PERSON and finally prints it on STDOUT.

To run a shell script, create a file with extension .sh, or if you are copying the .sh file from Windows to Linux the .sh file might not run and throw an error as follows:

echo "What is your name?"
read YourName
echo "Hello, $YourName"
chmod a+x

These are the basics of Shell Scripting, we have learned how to create, rename, delete, move files, directory, how to run a shell script in the blog.

Leave A Comment

Your email address will not be published. Required fields are marked *