Bash Introduction

Bash is a shell (command line environment) for Unix systems and Linux systems, and is the default shell for most Linux distributions.

The meaning of Shell

To learn Bash, you first need to understand what Shell is. The original meaning of the word Shell is "shell", which corresponds to the kernel (kernel). It is a metaphor for the layer outside the kernel, that is, the dialogue interface between the user and the kernel.

Specifically, the word Shell has multiple meanings.

First of all, Shell is a program that provides an environment for dialogue with users. This environment has only one command prompt, allowing users to enter commands from the keyboard, so it is also called the command line interface (command line interface, abbreviated as CLI). Shell receives the command entered by the user, sends the command to the operating system for execution, and returns the result to the user. In this book, unless otherwise specified, Shell refers to the command line environment.

Secondly, Shell is a command interpreter that interprets the commands entered by the user. It supports grammars such as variables, conditional judgments, and loop operations, so users can write various small programs with Shell commands, also known as scripts. These scripts are all executed by Shell's interpretation, not by compilation.

Finally, Shell is a toolbox that provides various small tools for users to conveniently use the functions of the operating system.

Shell types

There are many kinds of shells. As long as they can provide users with a command line environment, they can all be regarded as shells.

Historically, the main Shell has the following.

-Bourne Shell (sh) -Bourne Again shell (bash) -C Shell (csh) -TENEX C Shell (tcsh) -Korn shell (ksh) -Z Shell (zsh) -Friendly Interactive Shell (fish)

Bash is currently the most commonly used Shell. Unless otherwise specified, Shell and Bash below are used as synonyms and can be used interchangeably.

The following command can view the currently running Shell.

$ echo $SHELL

The following command can view all shells installed in the current Linux system.

$ cat /etc/shells

In the above two commands, $ is the prompt of the command line environment, and the user only needs to enter the content after the prompt.

Linux allows each user to use a different Shell, and the user's default Shell is generally Bash, or compatible with Bash.

Command line environment

Terminal emulator

If it is a Linux system without a graphical environment (such as a system dedicated to a server), it will be directly in the command line environment after startup.

However, most current Linux distributions, especially those aimed at ordinary users, are graphical environments. After the user logs in to the system, it automatically enters the graphical environment, and needs to start the terminal emulator to enter the command line environment.

The so-called "terminal emulator" (terminal emulator) is a program that simulates a command-line window, allowing users to use the command-line environment in a window, and provides various additional functions, such as adjusting color, font size, line spacing, and so on.

Different Linux distributions (or different desktop environments to be precise) have different terminal programs. For example, the terminal program of the KDE desktop environment is konsole, and the terminal program of the Gnome desktop environment is gnome-terminal. Users can also install the first Three-party terminal program. All terminal programs, despite their different names, have the same basic function, which is to allow users to enter the command line environment and use Shell.

Command line prompt

After entering the command line environment, the user will see the Shell prompt. The prompt is often a string of prefixes, and ends with a dollar sign $ at the end. Users can enter various commands after this sign.

[user@hostname] $

In the above example, the complete prompt is [user@hostname] $, where the prefix is ​​the user name (user) plus @, and then the host name (hostname). For example, the user name is bill, the host name is home-machine, and the prefix is ​​bill@home-machine.

Note that the prompt of the root user (root) does not end with a dollar sign ($), but ends with a pound sign (#) to remind users that they now have root privileges and can perform various operations. Be careful not to misuse. This symbol can be defined by yourself, see the chapter "Command Prompt" for details.

For the sake of brevity, the following command line prompts only use $.

Entry and exit methods

After entering the command line environment, Bash is usually opened. If your Shell is not Bash, you can enter the bash command to start Bash.

$ bash

To exit the Bash environment, you can use the exit command, or you can press Ctrl + d at the same time.

$ exit

The basic usage of Bash is to enter various commands on the command line, which is very intuitive. As an exercise, try typing the pwd command. Press the Enter key and the current directory will be displayed.

$ pwd

If you accidentally enter pwe, a prompt will be returned, indicating that the input is wrong and there is no corresponding executable program.

$ pwe
bash: pwe: command not found

The history of Shell and Bash

Shell was born with the birth of the Unix system.

In 1969, Ken Thompson and Dennis Ritchie developed the first version of Unix.

In 1971, Ken Thompson wrote the original Shell, called the Thompson shell, with the program name sh, which is convenient for users to use Unix.

Between 1973 and 1975, John R. Mashey extended the original Thompson shell and added programming features, making Shell a programming language. This version of the Shell is called the Mashey shell.

In 1976, Stephen Bourne combined the functions of the Mashey shell to rewrite a new shell called the Bourne shell.

In 1978, Bill Joy of the University of California at Berkeley developed the C shell, which provided the shell with the C language syntax. The program name was csh. It was the first UNIX shell that actually replaced sh and was merged into the 2BSD version of Berkeley UNIX.

In 1979, the seventh edition of UNIX was released, with the built-in Bourne Shell, which made it the default shell of Unix. Note that Thompson shell, Mashey shell and Bourne shell are all products of Bell Labs, and the program names are all sh. For users, they are the same thing, but the underlying code is different.

In 1983, David Korn developed the Korn shell, the program name is ksh.

In 1985, Richard Stallman established the Free Software Foundation (FSF). Since the copyright of Shell belongs to Bell, he decided to write a freely copyrighted Shell program that uses the GNU license to avoid Unix copyright disputes.

In 1988, Brian Fox, the first paid programmer of the Free Software Foundation, wrote a Shell. The function is basically a clone of the Bourne shell. It is called Bourne-Again SHell, or Bash for short. The program is called bash. Can be used for free. Later, it gradually became the standard Shell of the Linux system.

In 1989, Bash released version 1.0.

In 1996, Bash released version 2.0.

In 2004, Bash released version 3.0.

In 2009, Bash released version 4.0.

In 2019, Bash released version 5.0.

Users can check the Bash version of the machine through the --version parameter of the bash command or the environment variable $BASH_VERSION.

$ bash --version
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)

# Or