FahmidasClassroom

Learn by easy steps

Bp1

What is Bash?

The full form of Bash is Bourne-Again SHell. It is widely used in Unix shell, which provides a command-line interface for interacting with the operating system. It was developed as a free and open-source alternative to the original Unix shell, the Bourne shell (sh), which was developed by Stephen Bourne at Bell Labs in the 1970s. Bash was developed by Brian Fox at the Free Software Foundation in the late 1980s and early 1990s. It is now the default shell for most Linux distributions and macOS and is also available on Windows 10 through the Windows Subsystem for Linux (WSL). Bash provides a powerful set of commands and features for managing files, running programs, and automating tasks. It also supports scripting, allowing users to write scripts to automate repetitive tasks or perform complex operations. Bash scripts can be used for a variety of purposes, from simple one-liners to complex programs that manipulate data, interact with other programs or control system resources. One of the key features of Bash is its support for variables and command substitution. Variables can be used to store and manipulate data, while command substitution allows the output of one command to be used as input for another command. This makes it easy to chain commands together and perform complex operations with a single command. Another important feature of Bash is its support for shell scripts, which allow users to automate repetitive tasks or perform complex operations. Shell scripts can be written in any text editor and can be used to automate tasks such as backups, system maintenance, or data processing. Bash also supports job control, allowing users to run multiple commands simultaneously and manage their execution.

What are the benefits of using Bash?

It is a powerful and versatile tool that provides numerous benefits to users. Some of the benefits of Bash are mentioned here.

  1. One of the key benefits of using Bash is its ability to automate repetitive tasks. By writing scripts and running them in Bash, users can save time and increase productivity. For example, a script can be created to automatically back up files at regular intervals, or to perform system maintenance tasks.
  2. It is flexibility. With its large collection of built-in commands and utilities, it can be used to manage files and directories, extract and manipulate data, and even communicate with other systems.
  3. It supports regular expressions, which allows for powerful pattern matching and text manipulation.
  4. It is highly customizable. Users can create aliases for commonly used commands, define their functions, and set environment variables to personalize their shell experience.
  5. It allows users to execute commands and run scripts in a terminal window, giving them complete control over their system.
  6. It can be used to automate repetitive tasks through scripting. Users can write scripts that combine multiple commands and utilities to perform complex tasks quickly and efficiently.
  7. It can be extended through the use of plugins and third-party tools.

Who will learn Bash?

Bash is the go-to tool for automating repetitive tasks and managing large amounts of data in Unix-based systems. It is an essential tool for system administrators, developers, and power users who need to manage and automate tasks on their machines. System administrators use bash scripts to automate repetitive tasks, such as backups, system maintenance, and user management. Developers use bash scripts to automate their build and deployment processes. Even casual users can benefit from learning bash as it enables them to perform various tasks more efficiently, such as file management, searching, and sorting. It allows individuals to perform tasks more efficiently and effectively, which can lead to more streamlined workflows and increased productivity. Many companies require proficiency in bash as part of their job requirements for system administration and development roles. With the increasing demand for automation in the tech industry, proficiency in bash has become an essential skill for job seekers. So, anyone interested in managing and automating tasks in a Unix-based operating system should learn bash.

How to start working with Bash?

You can start to learn bash programming in multiple ways. The ways to write and execute bash script on Ubuntu and Windows operating system, and by using online interpreter have been shown here.

Bash on Ubuntu

Bash interpreter is installed by default on any Linux operating system. So, the user can start the bash programming from the terminal or by using any text or graphical editor on the Ubuntu operating system. Small bash scripts can be executed from the terminal easily. But when the user needs to write large scripts to develop any application then he/she has to use an editor. Vi, vim, Nano, etc. are examples of text editors. A text editor can be used as a graphical editor to write a bash script. The user can also install any popular graphical editor to write the bash script, such as Visual Studio Code.

Bash on Windows

There are two ways to use bash on Windows. One way is to use VirtualBox for installing the Ubuntu operating system on Windows and another way is to use the Ubuntu app that uses WSL. The virtual box and the ISO image of the Ubuntu have to be downloaded for installing Ubuntu on the virtual box. If you don’t want to use the virtualbox then you have to download the Ubuntu app from the Microsoft Store.

You can check this tutorial to learn the way to install Ubuntu on Virtualbox.

You can check this tutorial to learn the way to install the Ubuntu app on Windows.

Bash on the online interpreter

Many online interpreters are available to run bash scripts. If the user doesn’t want to use the Ubuntu operating system or WSL of Windows then he/she can use an online interpreter to learn bash programming. However, the online interpreter has some limitations. So, I recommend using the Ubuntu operating system or WSL or VirtualBox to learn bash.

Write the bash script

The extension of the bash file can be ‘.sh’ or ‘.bash’. You can create the bash file by using a text editor or graphical editor. The uses of both editors have been shown here. Press Alt+Ctrl+T to open the terminal to open any editor.

Write a script on the text editor

Many types of text editors exist on Ubuntu. Here, nano editor has been used. Run the following command to open the nano editor from the terminal to Create a bash file named ‘first_bash_script.bash’.

$ nano first_bash_script.bash

Add the following script to the file that will print a simple text and the text with a variable.

#!/bin/bash

#Print a simple text
echo "My first bash program."
#Declare a variable
var="Bash"
#Print the text with the variable
echo "$var scripting language."

Write a script on the graphical editor

You can open the text editor of Ubuntu as a simple graphical editor by pressing the icon of the text editor or by typing gedit from the terminal. After opening the editor, copy the content of the file that is given above and save the file.

Execute Bash Script

You can run the bash script by using the `bash` command or executable file of the bash script. Both ways of executing the bash script have been shown here.

Run the script using the `bash` command

Run the `bash` command with the file name to execute the script.

$ bash first_bash_script.bash

The output of the script has been shown here.

p1

Run the executable file of the script

You have to set the executable permission of the first_bash_script. bash file to execute the script without the `bash` command. The following `chmod` command will set the executable permission bit of the first_bash_script.bash file.

$ chmod a+x first_bash_script.bash

Run the executable file.

$ ./first_bash_script.bash

A similar output has been generated here.

p2

Exercise:

Create a bash file that defines a numeric variable named number and print the text, “The price of the book is $number”.

Summary

The purposes of learning Bash programming and the ways of creating and executing the Bash script in different platforms for the new Bash learners have been explained in this part of this chapter.