Shell Scripting executes commands to perform some useful functions and is designed to run in the shell. Shell scripts are quite handy to perform operations like file manipulation, automating tasks to avoid time consumption; you can even create your commands.
Shell is an interface between the user and operating system that lets users interact with the operating system and perform various tasks using commands. Shell takes input from the user through the terminal, interacts with the kernel, processes it, and gives the output.
How to create a shell script using Vim
Shell Scripts usually created using any text editor. Nano and Vim editors are well-known text editors to create bash scripting files. For this example, we are using “Vim”. If you do not have Vim, then install it using the command:
Open text editor using:
Create a new file:
Type the script. “#! /bin/bash” operator, shell directed to bourne shell: r
echo “Hello World”
And save the file, press the “Esc” key to switch the mode, and then press “:w” to save it. If it gives a “Read-only” error file, then use “:w!”, the file will be saved:
Now to execute the file, type:
How to add comments in a shell script
To add a comment, use the “#” operator; the syntax is given below:
#This is my first shell script
echo “Hello World”
How to use variables in a shell script
For any programming, language variables are essential. Variables are used to store a value, whether it is an integer, character, or text string. Let’s understand it with an example:
myvariable=“This is my first script”
The above script will give variable value as output:
Let’s check how to get value in a variable from user value from user:
echo “Enter your name”
echo “Enter your age”
echo “$name_varibale is $ age_variable old”
The following image is showing the output:
Shell scripting is very important to create your own command or run multiple commands with one script file to perform various functions. In this guide, we grasp the basic idea of shell scripting. We learned how to script and a shell script file, save it and then execute it. Moreover, we understood the usage of variables in shell scripting. Shell scripting is very handy to accomplish different useful tasks, and there is a lot to uncover.