Day 4 Basic Linux Shell Scripting

Day 4 Basic Linux Shell Scripting

What is Shell?

A shell is special user program which provide an interface to user to use operating system services. Shell accept human readable commands from user and convert them into something which kernel can understand. It is a command language interpreter that execute commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or start the terminal.

What is Shell Scripting?

Shell scripting refers to the process of creating and running a sequence of commands in a shell, which is a command-line interpreter for an operating system. In simpler terms, it involves writing a series of commands in a script file to automate tasks that would normally be executed one by one in a command-line interface.

The shell is a program that takes your commands from the keyboard and gives them to the operating system to perform. Common shells include Bash (Bourne Again SHell), sh (Bourne Shell), csh (C Shell), and others, each with its own syntax and features.

Shell scripts are often used for various purposes, including:

  1. Automation: Performing repetitive tasks or a series of commands automatically.

  2. System Administration: Managing and configuring system settings and resources.

  3. File Operations: Copying, moving, renaming, or deleting files and directories.

  4. Data Processing: Parsing and manipulating data, such as log files or text streams.

  5. User Interaction: Creating interactive command-line interfaces.

Here's a simple example of a Linux Bash script:

#!/bin/bash

echo "Enter your name: "
read name

echo "Hello, $name! This is a simple Bash script."

Type of Shell Scripting:-

  1. Bash (Bourne Again SHell): Bash is one of the most widely used shells and is the default shell on many Linux distributions. It is an extended version of the original Bourne Shell (sh) and includes additional features, making it powerful and user-friendly.

  2. sh (Bourne Shell): The Bourne Shell is one of the oldest and simplest shells. It serves as the foundation for many other shells. While it lacks some of the features found in more modern shells, it is still used in some contexts for its simplicity.

  3. csh (C Shell): The C Shell is known for its syntax, which resembles the C programming language. It includes interactive features like command-line history and a C-like syntax, making it popular among some users.

  4. ksh (Korn Shell): The Korn Shell is an improvement over the Bourne Shell with additional features, including command history and advanced scripting capabilities. It aims to combine the best features of the Bourne Shell and the C Shell.

  5. zsh (Z Shell): The Z Shell is an extended and improved version of the Bourne Shell. It includes advanced features such as powerful tab-completion, spelling correction, and support for themes. Zsh is highly customizable and is gaining popularity among users who want a feature-rich shell.

  6. Fish (Friendly Interactive SHell): Fish is designed to be user-friendly and interactive. It provides syntax highlighting, autosuggestions, and an easy-to-understand scripting language. Fish aims to be approachable for new users while still offering powerful features.

Some Example:-

  1. What is #!/bin/bash? can we write #!/bin/sh as well?

    #!/bin/bash or #!/bin/sh at the beginning of a script is called a shebang line. It indicates the path to the shell interpreter that should be used to execute the script.

    • #!/bin/bash: Specifies that the Bash shell should be used.

    • #!/bin/sh: Specifies that the default system shell (often a POSIX-compliant shell) should be used.

Both shebang lines are common, and you can use either depending on your preferences and requirement.