Hello, readers, I hope you are all doing great and learning new and amazing stuff. So to make some additions to that learning we are going to cover Shell Scripting or also know as Bash Scripting over here in this article we will cover Introduction to Shell Scripting.
Purpose of this series:
The main purpose of this series is to help people that are looking into learning Bash programming and give them a starting point and an understanding of Bourne Shell programming and how simple and powerful it is, as a reference there will be examples and exercises that you can try to solve.
Getting things up and running:
By the time of this article, the current version of Bash is Version 5 released in January 2019. After you have set up your environment always check for the latest version.
A Brief History of sh
Steve Bourne wrote the Bourne shell which appeared in the Seventh Edition Bell Labs Research version of Unix. Many other shells have been written, this particular tutorial concentrates on the Bourne and the Bourne Again shells. Other shells include the Korn Shell (ksh), the C Shell (csh), and variations such as tcsh, Z-shell (zsh). This tutorial does not cover those shells.
If you are reading this that means that you already have some previous knowledge about Linux:
- Use of an interactive Unix/Linux shell
- Minimal programming knowledge – use of variables, functions, is useful background knowledge
- Understanding of some Unix/Linux commands, and competence in using some of the more common ones. (ls, cp, echo, etc)
- Programmers of Ruby, Perl, Python, C, Pascal, or any programming language (even BASIC) who can maybe read shell scripts, but don’t feel they understand exactly how they work.
How the Code looks:
Every Bash code starts with:
This tells the system that its a shell script and will execute it using the Bourne shell. To display some text on the terminal screen “echo” is used.
Once you are done with your code we have to change the file permissions of our script file and assign execution permission to our script by using this command:
chmod 755 script.sh
chmod +x script.sh
Here is a simple Bash script that will print out “Hello World“:
#!/bin/sh echo Hello World
Save it as script.sh give execution permission to the file:
chmod +x script.sh
then in the terminal type:
After you run this script you will see this Output:
Comments in the Script:
So if you decide to add comments while writing your code to be neat you can do that by using “#” before the actual comment:
#!/bin/sh # This is a comment! echo Hello World # This will Print Hello World!
As a final example, type in the following script. Try to predict the outcome before you run it:
#!/bin/sh # This is a comment! echo "Hello World" # This is a comment, too! echo "Hello World" echo "Hello * World" echo Hello * World echo Hello World echo "Hello" World echo Hello " " World echo "Hello "*" World" echo `hello` world echo 'hello' world
This is just a basic overview about Bash scripting and will be covering more of it in future if you do like it and want us to cover more about it do let us know in the comments and do share it with other so if they are interested in learning they can also get started.