What is Bash (Bourne Again Shell)?

0
4
VMware CEO tells enterprises to become 'cloud-smart' to speed up pace of digital transformation

Source is ComputerWeekly.com

Bash (Bourne Again Shell) is the free and enhanced version of the Bourne shell distributed with Linux and GNU operating systems (OSes). Bash is similar to the original but has added features, such as command-line editing.

Created to improve on the earlier Bourne shell, named sh, Bash includes features from the Korn shell and the C shell. Bash is intended to conform to the shell standard specified as part of IEEE Portable Operating System Interface. A command language script written for the Bourne shell should also run in the Bash shell.

Bash is released under the GNU General Public License. It is available for most versions of Unix and Linux, and versions have been ported to Microsoft Disk Operating System and Windows.

As explained in the “Bash Reference Manual,” the name Bash is an acronym of Bourne Again Shell, which is a pun on Stephen Bourne, author of the Bourne shell. Bash is a superset of the earlier shell and is generally compatible with Bourne shell programs.

What is a shell?

In computing, a shell program provides access to an OS’ components. The shell gives users or other programs a way to get inside the system; the shell defines the boundary between inside and outside.

There are two types of OS shells:

  1. Command-line interface (CLI) shells, like Bash, offer users a concise and efficient mode of interacting with the OS without requiring the overhead of a graphical user interface.
  2. Graphical user interface shells, such as Windows and macOS, are considered easier for beginners to use but usually also offer programs that emulate a CLI-based shell for system administrators or other power users who prefer to interact with a command prompt.

Bash is the most commonly used CLI shell for Unix-based OSes, including Linux.

What is Bash used for?

Bash, like other CLIs, is used for any computer application that requires precision when working with files and data, especially where large numbers of files or large quantities of data need to be searched, sorted, manipulated or processed in any way.

Some of the most common Bash use cases are the following:

  • System administrators use Bash to manage systems systematically and reproducibly. System administrators use Bash to troubleshoot systems that are not functioning as desired or expected by logging in to systems and reviewing system configurations and network connections. System administrators also rely on Bash scripts to distribute software updates and patches, monitor running systems, and update and configure systems.
  • Software developers rely on Bash for many development tasks. Bash can be used to automate software development tasks, such as code compilation, debugging and software testing.
  • Network engineers use Bash to test, configure and optimize on organizational networks.
  • Computer science researchers use Bash to manage research systems and to carry out research on those systems.
  • Hobbyists and power users use Bash to interact with their systems, execute programs and maintain their systems.

Bash is commonly used interactively, but it can also be used to write shell scripts. Almost any computer task can be automated using a Bash script. Bash scripts can be run on demand or scheduled to run periodically.

How does Bash work?

At first sight, Bash appears to be a simple command/response system, where users enter commands and Bash returns the results after those commands are run. However, Bash is also a programming platform, and users can write programs that accept input and produce output using shell commands in shell scripts. Types of Bash commands include the following:

  • Simple commands, which are usually run by themselves or with parameters and variables.
  • Pipes, which are used to link the output of one or more commands as input to other commands.
  • Lists, which enable users to run multiple commands in sequence.
  • Compound commands, which enable script programming and include loops for repeating a command a specific number of times and conditional constructs for running commands only when a specific condition is met.

One of the most basic Bash commands, ls, does one thing: list directory contents. By itself, this command lists only the names of files and subdirectories in the current working directory.

Figure 1

The ls command has numerous parameters that modify how the results are displayed. Figure 3 shows some frequently used parameters for the ls command.

Bash ls command parameters
Figure 2

Used all together, these parameters give the user a much clearer sense of what files and subdirectories are in a directory, when they have last been changed and by whom.

Bash piping pipes
Figure 3

Bash enables combining commands by piping output of one command to be used as the input for another command. For example, the following command can be used to list all files on a file system using the -R parameter to specify the listing should be recursive.

user@hostname:/$ 1s -1ashR

The response to this command, especially when issued from the system root directory, contains too many entries for humans to parse easily. This is where pipes can come into play; in this case, the user can pipe the output from the ls command to the Bash text pattern matching command grep.

The pipe symbol — vertical bar (|) — directs output from the directory listing into the grep command to return only files and subdirectories with file names that include the specified text pattern. This command returns only files that include the string filename.txt, so this command can be used to locate a specific file.

user@hostname:/$ 1s -1ashR |grep 'filename.txt'

Other common Bash commands include the following:

  • date. It displays the current date.
  • touch. It creates a new file.
  • pwd. It displays the present working directory.
  • echo. It prints a string of text or a variable value to the terminal.
  • man. It opens the commands manual.
  • which. It displays the Bash shell path.
  • df. It displays the available disk space.
  • cp. It copies a file or directory.
  • chmod. It changes the permissions of a file or directory.

What are the benefits of Bash?

Bash is an open source application, allowing anyone to use it for a variety of system-related tasks, including automation. It provides a simple syntax that makes it easy to use. It also provides several convenience-focused functions that further simplify its use. However, these same functions can also cause compatibility issues on systems that are not running Bash.

Command-line editing is one special Bash feature that’s not always available with other CLIs. Bash retains a command history, which can be accessed by pressing the up arrow key. This makes it easier to precisely rerun a command. These prior commands can also be modified at the command line, using special keys to copy, paste, delete or modify a prior command.

What is Bash scripting?

With Bash serving as a scriptable shell and programming language, users can script Bash commands into a text file and then run it in Bash. The system executes the file automatically, removing the need for the user to manually type and run commands at the Bash prompt. Bash scripts provide a way to automate many repetitive tasks, such as the following:

  • Clean up log files.
  • Take data backups.
  • Convert temporary files into specific directories.
  • Create folders.
  • Navigate to a particular directory.
  • Execute a specific process.
  • Manage system resources.

Advantages of Bash scripting

Almost any routine task in Unix and Linux systems can be scripted through Bash, enabling many different types of automations and saving users time and effort. Bash scripts can also be run on Windows and macOS systems for automating repetitive tasks by using emulators or virtual machines.

That said, Bash is not limited to simple automations. Users can also set up complex automation tasks by integrating Bash scripts with databases, web servers, and other tools and applications.

Bash scripting also minimizes the potential for errors since little human effort is required to perform a task. As long as there are no errors in the script, Bash runs it and returns the expected outcomes.

Bash scripts are easy to write and edit. A simple text editor can be used for both purposes. They are also easy to debug. Setting the -x option at the beginning of the script enables debugging mode and enables users to identify where errors are occurring in the script. The scripts can also be customized to suit specific requirements. Users can also combine Bash scripts with other utilities to create more powerful scripts.

How to get started with Bash scripting

The starting point for Bash scripting is always the command prompt. It looks like this.

[username@host ~]$

Users can enter any command after the $ sign. The following is the syntax to be used.

command [OPTIONS] arguments

Bash scripts usually end with the .sh extension. However, the extension is not mandatory, and scripts can run without it.

The first line of every Bash script always includes a combination of # and ! followed by the Bash shell path. The combination is known as shebang, and it provides an absolute path to the Bash interpreter.

Subsequent lines in the script can include the following:

  • Commands.
  • Valid paths.
  • Variables.

When creating a Bash script that includes variables, users must follow standard naming conventions. For example, variable names should not contain spaces; they should always start with a letter or an underscore.

The script is only executable if the user is assigned execution rights — if they don’t already have it. This assignation can be done using the following command.

chmod u+x run_all.sh

As with many other languages, Bash also allows users to insert comments inside scripts, say, to document their actions or to help other users to understand the code. In Bash, all comments start with a # and are ignored for execution by the Bash interpreter.

Bash is one of the foundations of modern system and network administration, and new users face a learning curve when using it. However, once learned, Bash skills are forever: A time-traveling system administrator from 1992 would likely be able to get right back to work on a modern Linux system, using Bash. Learn more about Bash scripting from this tutorial on creating a Bash shell that accepts arguments when it runs.

Source is ComputerWeekly.com

Vorig artikelDatacentre outages decreasing in frequency, Uptime Institute Intelligence data shows
Volgend artikelCloud storage for AI: Options, pros and cons