Bash Scripting Basics

Bash Scripting is an important part of system administration and development, which is used by the system administrators, Network Engineers, Developers, and everyone else who uses the Linux/Unix operating system. They use Bash to create custom scripts (a set of commands) and perform automated tasks.

Bash Script

A bash shell script can be defined as a text file containing one or more commands, which we usually run in a command-line interface. It might include the hallmarks of imperative programming such as conditional constructs, loops, and functions, etc. It is generally used to automate repetitive tasks on Linux/ Unix systems. In other words, we can say that:
"A bash script is a computer program which is actually written in the bash programming language."

Bash Script Writing

First of all, select the directory using the cd command in which you want to create your bash script. Use the text editor like gedit in which you can type the shell commands.
To write an empty bash script, you need to follow the steps given below:

Step 1

Use ‘touch’ command to create the empty (zero-byte size) script.
touch file_name

Step 2

To open the created script in the text editor (e.g., gedit), type
gedit file_name.sh
Here, .sh is an extension for the script files, which should be used for the proper execution.
It will open a script file in a text editor. Type your shell commands in the newly opened text window. Before writing your bash shell commands in a script, you are required to use the base line of bash scripts.
Every bash script should start with a line-
#!/bin/bash
Where "#!" is known as the "shebang”, and the rest of the part of the line is known as the path to the interpreter, which specifies the location of the bash shell in the operating system.
It is the way to write a bash script.

Bash Script: Echo Command

An ‘echo’ is one of the most used built-in commands in Bash. It is used to display the standard output by passing the arguments or commands. It is the widely used command for getting output on the screen. 

Note: The performance of ‘echo’ command in a terminal and bash script is the same.

Syntax

echo [option] [string]
echo [string]
This command can also be used to insert space between two lines. To insert a space, type ‘echo’ between outputs. You will understand the use of echo to insert a space in the following articles.

Run Bash Script

Method 1

By default, newly created files can not be executed regardless of its file extension suffix. Therefore, you are required to change the file permission for the specific files and make executable. To assign the executable access to the file, use the following command:
chmod +x file_name.sh
At the end, you can execute/run the bash script prefixing with characters ./. For example-
./file_name.sh

Method 2

Another way to execute the bash scripts is to call the interpreter explicitly. For example-
bash file_name.sh
This method does not need the declaration of shebang. There is no need to make the script executable for this method. When the bash executable binary is explicitly called, the content of the file is loaded and interpreted as Bash Shell Script.

←prev next→


Tutorial Highlights


Popular Tutorials


Blog with us

CONNECT WITH US

Like Tutorialsmate on Facebook Follow Tutorialsmate on Facebook Add Tutorialsmate on Facebook Subscribe Tutorialsmate on Facebook Follow Tutorialsmate on Facebook Follow Tutorialsmate on Facebook
Get tutorialsmate on Google Play
© 2020 TutorialsMate. Designed by TutorialsMate