Shell Scripting

Shell scripting in Linux refers to the process of writing and executing scripts using a shell (command-line interface) as the scripting language. It is a series of commands written in a plain text file that can be executed as a single unit.
Shell scripts are widely used in Linux for automating tasks, performing system administration tasks, and creating complex workflows. They offer the ability to combine multiple commands, control flow using conditional statements and loops, handle input and output, and interact with the file system.
Shell scripts are written in scripting languages supported by shells like Bash, Zsh, Ksh, or Csh. These languages provide features like variables, functions, control structures, and string manipulation to enhance the capabilities of shell scripting.

Pre-requisite of Shell Scripting

  1. Linux Environment: You need access to a Linux system or a Linux-based operating system like Ubuntu, Fedora, CentOS, or Debian.
  2. Terminal or Shell: You should have a terminal or shell application to execute shell commands and run shell scripts. The default terminal in Linux distributions is usually sufficient.
  3. Basic Command-line Knowledge: Familiarize yourself with basic Linux command-line operations, such as navigating the file system, creating and editing files, and executing commands.
  4. Shell Interpreter: Different shells are available in Linux, such as Bash, Zsh, Ksh, and Csh. Choose a shell and ensure it is installed on your system.
  5. Text Editor: Use a text editor to write and edit shell scripts. Popular options include Vim, Nano, Emacs, or any other text editor of your choice.
  6. Script Execution Permission: Ensure that the script files have the appropriate permissions to be executed. Use the chmod command to grant executable permissions (chmod +x script.sh).
  7. Scripting Language: Shell scripts are typically written using scripting languages supported by the chosen shell (e.g., Bash). Learn the syntax, features, and conventions of the scripting language.

How to print Hello World in Shell Scripting

To print “Hello, World!” in a Linux shell script, you can use the echo command. Here’s an example script that accomplishes this:

  1. Create a new file and open it in a text editor.
  2. Copy the above script and paste it into the file.
  3. Save the file with a .sh extension, such as hello.sh.
  4. Open a terminal or shell.
  5. Navigate to the directory where the script is saved.
  6. Make the script executable by running the following command: chmod +x hello.sh.
  7. Execute the script by running the following command: ./hello.sh.
  8. After executing the script, you should see “Hello, World!” printed in the terminal. This basic example demonstrates the use of the echo command to output a string in a shell script.

Here is an example of Shell Scripting where we have printed “Hello World!”.

Shell Scripting

In shell scripting, a “shebang” (also known as a hashbang or a hashpling) is a special sequence of characters at the beginning of a script file. It consists of the characters “#!” followed by the path to the interpreter where this should be used to execute the script.
For example, the shebang “#!/bin/bash” indicates that the script should be interpreted and executed by the Bash shell. Similarly, “#!/usr/bin/perl” specifies that the script should be executed by the Perl interpreter.
The shebang allows you to specify the interpreter for the script, regardless of the default shell or interpreter set on the system.

Here is an example of “she-bang” in Shell Scripting:

In shell scripting, “comments” are lines of text that are ignored by the interpreter when executing the script. They are used to add explanations, documentation, or notes within the script to improve readability and provide information about the script’s purpose, usage, or specific sections.
Comments in shell scripts serve as non-executable text and are not processed or executed as part of the script. They are solely meant for human readers to understand the code.

In most shells, including Bash, there are two common ways to write comments:

Single-line comments: These comments begin with the “#” (hash) character and continue until the end of the line. Anything after the “#” on the same line is considered a comment and is ignored by the interpreter. For example:

Multi-line comments: While shells like Bash do not have built-in multi-line comment syntax, you can achieve a similar effect using here documents or enclosing the code within a conditional block that is always false. For example:

In shell scripting, variables are used to store and manipulate data. They are symbolic names that represent values or expressions. Variables allow you to store information that can be referenced and modified throughout the script. We have already discussed on this topic earlier, please refer to variable section on this website itself.

In shell scripting, the “test” command is used to evaluate conditions and perform tests. It is also known as the [ command, as it is commonly used with square brackets ( ‘[‘ and ‘]’ ) for condition testing. The “test” command allows you to check various conditions, such as file existence, file types, string comparisons, numerical comparisons, and more. It returns an exit status of 0 (true) or 1 (false) based on the result of the test.

Here is an example of “test” in Linux. The “test” command returns 1 if the test fails or 0 if pass.

Here are some common usages of the test" command:

FlagsUsage or Description
File Tests: 
-e file:Checks if file exists.
-f file:Checks if file is a regular file.
-d directory:Checks if directory is a directory.
-r file:Checks if file is readable.
-w file:Checks if file is writable.
-x file:Checks if file is executable.
String Tests: 
-z string:Checks if string is empty.
-n string:Checks if string is not empty.
string1 = string2:Checks if string1 is equal to string2.
string1 != string2:Checks if string1 is not equal to string2.
Numerical Tests: 
num1 -eq num2:Checks if num1 is equal to num2.
num1 -ne num2:Checks if num1 is not equal to num2.
num1 -lt num2:Checks if num1 is less than num2.
num1 -gt num2:Checks if num1 is greater than num2.
num1 -le num2:Checks if num1 is less than or equal to num2.
num1 -ge num2:Checks if num1 is greater than or equal to num2.
Test flags in Shell Scripting

In shell scripting, the if-then-else statement is used to conditionally execute a block of code based on a specified condition. It allows you to control the flow of the script by branching the execution path depending on whether the condition evaluates to true or false.The general syntax of the if-then-else statement in shell scripting is as follows:

Here’s how the if-then-else statement works:
The condition is an expression or test that is evaluated. It can be a command, a comparison, or a test using the test command or brackets [ ].
If the condition evaluates to true (exit status 0), the code block following the then keyword is executed.
If the condition evaluates to false (non-zero exit status), the code block following the else keyword (if present) is executed.
The fi keyword marks the end of the if-then-else block.

Here’s an example to illustrate the usage of if-then-else in a shell script:

Wrapping Up

If you wish to learn more on Shell Scripting, please do follow this URL or wish to learn more on Linux on different topics, please follow this URL.