One of the most important components in Linux distributions is the Bash command shell. It performs functions of reading and running commands, allows you to work with files as well as execute scripts. For the full functioning of operating systems of the Linux family, using Bash also plays an important role.
This review contains important information that will be useful for anyone just getting started with Linux.
What is Bash? Bash meaning.
Bash (Bourne again shell) – an improved version of the Bourne-shell software shell. It is a command processor that runs interactively in a text window. Bash is used for:
- accepting user commands,
- sending them to the operating system for the next processing.
Bash is also a scripting programming language (it allows the creation of scripts to automate various operations). Using Bash in some cases allows you to avoid installing specialized software, since Bash is a convenient and multifunctional tool for performing a wide range of tasks.
History of Bash creation
In 1978, British scientist Stephen Bourne created the Bourne-shell (previous version of Bash). It became one of the first shells for Unix operating systems.
In 1987, programmer Brian Fox decided to increase the number of functions performed and improve Bourne-shell as a whole. A beta version of Bash was created in 1989. Brian Fox continued to maintain and improve his development until 1993.
Brian Fox is the creator of the Bash routine.
The main features, commands and rules for writing them from the original sh shell were transferred to Bash. Other features were taken from Korn Shell and C Shell. It should also be noted that Bash almost 100 percent met the POSIX standards.
A special terminal (including gnome-terminal, lxterm, eterm, xterm) provides interaction between the shell and the operating system. The Bash shell is pre-installed on most Unix/Linux distributions.
Bash writing rules are an extended Bourne-shell syntax. The Bash Reference Manual offers everything you need to know about the basics of interacting with the shell (including specification of individual commands).
Let's consider an example of a script that displays the inscription “Hello world”:
echo 'Hello World!'
This script consists of two lines:
- First line tells the operating system about the necessity to run the file using the Bash shell.
- Second line specifies an action to be performed by the script. This action should print the appropriate message in the terminal window.
Difference between Bash and Bourne-shell
Almost all important Bourne-shell scripts can be executed by Bash on Linux without any modifications. only scripts that require Bourne-shell embedded commands are exceptions.
Bash syntax uses techniques from the Korn Shell and C Shell:
- Command line editing;
Rules for replacing commands
- Output of command history;
- Creating directory stacks.
The Bash console can also be used as an interactive interpreter to autocomplete names of files, commands, variables and programs by pressing the Tab (↹) key.
Applying Bash. What is Bash for?
Examples of using Bash:
- Clear the terminal screen.
- Output the specified number of lines from the log.
- Find and create a selection of keywords, saving them in a new file.
- Recursively replace the owners of the specified files and folders.
- Create an archive of a directory with files and send it via a specific network protocol to another computer on the same network.
- Find duplicate files on the disk, display a list of them, and delete them if needed.
- Configure the system for creating backup copies of database files using dumping.
- Send a request for the configuration of other computers on the network.
- Suspend running tasks and restart previously suspended ones.
Despite the fact that many different variations of Bourne-Shell have been developed today, the Bash command interpreter still has a number of advantages over them:
- Supports working with arrays in Linux.
<<<“here strings’’ syntax.
Supports coprocesses with
Provides an opportunity to work with
[[structures (in sh only
[is available with restrictions).
$FUNCNAMEare extensions in Bash.
Many extensions available using C standards (including an ability to assign increment
+=, three-argument loops
- A large set of extensions for non-standard configurations is available (+ changing the register).
Redirection aliases available, like
- Significantly increased arithmetic capabilities.
- Interactive features are available in large numbers.
Principle of working with Bash
Using the example of connecting to a web server (where the site files are located) with an installed Linux distribution via SSH from scratch, let's consider how to use Bash.
We are considering a server with Ubuntu (the principle is the same for other distributions):
- The Linux Terminal program automatically launches Bash (after entering login and password).
The invitation is displayed basically as standard and looks like this:
<username>@<computer name>:<current directory><sign>.
In the example:
- the username is blurred;
vPro– computer name;
The grapheme tilde
~in Linux means the user's home folder in short form (/home/user);
$– indicates that the user does not have administrative rights (hash sign
#is displayed if the user is running as an administrator).
After sending the invitation, the terminal will automatically put a space and will wait for commands to be entered. In the image below the “Enter” was pressed. The next line contains the command
cdto move to the existing directory /opt. The last line shows the result of the output – the working folder has become /opt and this is displayed in the invitation.
Using the Linux command line, it can be possible to run any application installed on the system. Directories with a list of executable programs are usually located in the /bin, /usr/bin folders. The interpreter stores the checked directories in the
$PATHvariable. their list can be viewed by using the following command:
You can also use the
lscommand and add one of the directories found in
$PATHto have an opportunity to see a list of available programs.
If it is a console application, Bash will run it in the terminal. An example of launching the Nano editor is considered below.
Bash also has an ability to run apps in the background. To do this, you should add an ampersand
&at the end after entering the program name.
 23258is the process number and PID. Nano is in the background. You can return to it using the command
Considering that a wide range of commands can work in the background, you can check the number of the required one using the
The interpreter has many more useful embedded commands. Using these commands, you will be able to perform all the necessary workflows with the web server. For working with arrays and lines of text data in the console environment of Unix operating systems, you can use some of these commands.