Configure
Build your server
5.0
Rated excellent

Working basics with the bash command shell in Linux

Working basics with the bash command shell in Linux 26 September 2023

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.

What is Bash? Bash meaning

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

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.

Syntax

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).

Example script

Let's consider an example of a script that displays the inscription “Hello world”:

#!/usr/bin/env bash
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.

Example bash script

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:

  • Variables $PPID and $RANDOM;
  • 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?

Working basics with the bash command shell in Linux

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.

Important advantages

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.
  • Supports the <<< “here strings’’ syntax.
  • Supports coprocesses with <> redirection.
  • Works with .{png,jpg} extensions.
  • Provides an opportunity to work with [[ structures (in sh only [ is available with restrictions).
  • The variables $RANDOM, $SECONDS, $PIPESTATUS[@] and $FUNCNAME are extensions in Bash.
  • Many extensions available using C standards (including an ability to assign increment +=, three-argument loops for((i=0;i<=3;i++)) etc.).
  • A large set of extensions for non-standard configurations is available (+ changing the register).
  • Redirection aliases available, like Csh, like &| for 2>&1 | and &> for >...2>&1.
  • 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.

Working basics with the bash command shell in Linux

We are considering a server with Ubuntu (the principle is the same for other distributions):

  1. The Linux Terminal program automatically launches Bash (after entering login and password).
  2. The invitation is displayed basically as standard and looks like this:

    <username>@<computer name>:<current directory><sign>.

    Working basics with the bash command shell in Linux

    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).
  3. 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 cd to 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.

    Working basics with the bash command shell in Linux

  4. 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 $PATH variable. their list can be viewed by using the following command:

    echo $PATH

  5. You can also use the ls command and add one of the directories found in $PATH to have an opportunity to see a list of available programs.

    Working basics with the bash command shell in Linux

  6. If it is a console application, Bash will run it in the terminal. An example of launching the Nano editor is considered below.

    Working basics with the bash command shell in Linux

  7. 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.

    Working basics with the bash command shell in Linux

    The line [2] 23258 is the process number and PID. Nano is in the background. You can return to it using the command jobs or fg.

    Working basics with the bash command shell in Linux

    Considering that a wide range of commands can work in the background, you can check the number of the required one using the bg command.

    Working basics with the bash command shell in Linux

Conclusion

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.