Hi Everyone! In this blog, we are going to learn about Shell Scripting and how to get started with it in few simple steps. This tutorial helps in gaining a basic understanding of the concept of LINUX/UNIX shell scripting.
First, Before diving into how, let us first look at what Shell and Shell Scripting actually is.
What is a Shell?
A shell is simply a program which provides the interface between a user and an operating system. The operating system starts a shell, whenever a user logs in. In other words, it is an environment where we can run our commands, programs and scripts. However, If you want more information about Linux shells, kindly refer to this article.
What is Shell Scripting?
- Shell Scripting is an open-source computer program designed to be run by the Unix/Linux shell.
- Altogether, it is a program to write a series of commands for the shell to execute.
- This can help combine lengthy and repetitive sequences of commands into a single and simple script.
- Also, it can be stored and executed anytime which in turn reduces programming efforts.
Now that we know what a shell script is, let’s look at the steps using which you can easily get started.
1. Create a file using your preferred text editor
Shell Scripts are written using a text editor. First thing, open any text editor of your choice on your LINUX system such as vi, vim, or any other. Then, create a file with a name and the extension .sh and save it on a location where the shell can find it.
2. Name the script file with extension .sh
Shell Scripts are generally saved with the .sh extension. It describes what type of shell you are using in your LINUX system. For example, Bourne, POSIX, Korn shells are known as
sh. While C shell and Tops C shell are known
tcsh respectively. And Bourne Again Shell is known as
bash. In this tutorial, we are discussing
sh shell-based scripting.
This is how you can create a script file –
$ vi DemoScript.sh
3. Start the script with #!/bin/sh and write some code
What is #!/bin/sh exactly ?
Well, “#!” is an operator called shebang which directs the script to the interpreter location. It is nothing but the absolute path to the interpreter. Shebang consists of the two things –
- a number sign and an exclamation point character (#!)
- full path to the interpreter such as /bin/sh
Why is it needed?
Every Linux script is executed with this interpreter specified on a first line. Adding this ensures that Bash will be used to interpret the script, even if it is executed under another shell. To conclude, when we use “#!/bin/sh” the script gets directed to the Bourne shell.
Now, let’s create a simple Hello World script with what we have learned so far.
#!/bin/sh echo "Hello World!"
4. Save the script file with filename.sh
After you are done writing your code, just save your file with a name and the .sh extension. We’re almost done and now it’s time to execute the script.
5. For executing the script, type sh filename.sh
Finally, after saving your file, type the command
sh filename.sh on your terminal and voila, you will see the output on your screen as shown below –
$ sh DemoScript.sh Hello World!
I hope this article gave you some insights on shell scripting. Please feel free to drop any questions or suggestions in the comments. Thank You.
1 thought on “Getting started with Shell Scripting in 5 easy steps3 min read”
Comments are closed.