loader

This Bash scipting tutorial is designed for beginners and Intermediate levels. You will learn all of concepts that we may face in Bash Scripting. Also, we will provide an advanced course for administrators and specialists who wants to learn Bash Scripting for working with specific fields like Linux Administration and cisco automation etc. Before learning Bash Shell, you must have basic knowledge of the Linux Operating system.

If you are not familiar with Linux, I recommend you have a review of LPIC1 course in SMENODE.com at first!

What is a bash script?

Bash is a command language interpreter. It is widely available on various operating systems and is a default command interpreter on most GNU/Linux systems. There are different types of shells available in Linux Operating Systems. Some of them are as follows:

  1. Bourne Shell (sh)
  2. The Z Shell (zsh)
  3. The C Shell (csh)
  4. Korn Shell (ksh)
  5. GNU Bourne Shell (bash)

For finding which shell types are supported in your operating system, type the below command into the terminal:

				
					amir@server:~$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash

				
			

And to know where is bash location in your OS, type the below command and you will get a specific location:

				
					amir@server:~$ which bash
/usr/bin/bash          

				
			

A Bash script is a plain text file which contains a series of commands (like mv or cp). Anything you can run normally on the command line can be write into a script

How to Identify a Bash Script?

Usually bash scripts end with a .sh (for example myscript.sh). however, you should know that extension is not important in Linux operating system and it just help us to guess a file type by seeing the name of file, so a script doesn’t necessarily have to have this extension in order to work.

How to Create the First Bash Script?

Let’s create the first script in bash that outputs Hello World.

Create a file named hello_world.sh

touch hello_world.sh

Our script will look something like this:

				
					#!usr/bin/bash
echo "Hello World"

				
			

Edit the file hello_world.sh using a text editor of your choice and add the above lines in it.

  • Line 1 – Is what’s referred to as the shebang.

Shebang is a combination of bash # and bang !  followed the bash shell path. This is the first line of the script. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

  • Line 2 – Is the command echo which will print a message to the screen. You can type this command yourself on the command line and it will behave the same.

  How to run a bash script?

We can run a bash script in two different ways – one way is to call the script directly using the right interpreter, such as the following:

				
					amir@server:~$ bash hello_world.sh
Hello World!

				
			

This way makes sure we are using the right interpreter for the script.

Now, for another way of running the script we should make the script executable at first, and then run it:

				
					amir@server:~/$ chmod u+x hello_world.sh
amir@server:~/scripting$ ./hello_world.sh
Hello World!

				
			

We should know about difference between these two types; however, the result is same when we are running the same script.

In the first example, we are explicitly telling the system to use a particular interpreter and run the script. In the second example, we are telling the system to run the script using whatever interpreter it needs, and, in this way, the current shell takes the first line (the shebang) and try to find the interpreter that this line points to. If it finds it, the system uses this interpreter to run whatever is in the rest of the file.

how do you create a script?

Before writing a script, we should have a plan and think about below matters:

  • what is the task.
  • what commands I should use for the task.
  • what permissions is required for the individual commands to be successful.

 

Comments in BASH
if we use the # character anywhere in the script, an interpreter is going to treat

everything that comes after that character in the same line as a comment.

We should use the comments in different parts of our script. For example:

  • who wrote the script and when was it created.
  • Version of the script
  • Explain some part of the code

 

There are two types of comments:

  1. Single-line comment
  2. Multi-line comment

– Single-line comments: A single-line comment starts with hashtag symbol with no white spaces (#) and lasts till the end of the line. If the comment exceeds one line, then put a hashtag on the next line and continue the comment.

Example:

				
					#This is a comment
				
			

– Multi-line comments:

Also, there is something called a heredoc notation that we use when we need to create large blocks of comments. This notation uses shell redirection in a very specific way so that it can provide the header and the footer for the comment block.

				
					#!/bin/bash
<<<COMMENTBLOCK
First Comment line
Second Comment line
Another comment line
COMMENTBLOCK

				
			

COMMENTBLOCK shows start and end of the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *