Shell and the relationship between shell and bash

1. What is a shell

The shell is the interface program between you (the user) and Linux (or more accurately, you and the Linux kernel). Every command you enter at the prompt is interpreted by the shell and then passed to the Linux kernel.

The shell is a command-language interpreter. Has its own built-in shell command set. In addition, the shell can also be called by other effective Linux utilities and application programs in the system.

Whenever you type a command, it is interpreted by the Linux shell. Some commands, such as the print current working directory command (pwd), are included in Linux bash (just like the internal commands of DOS). Other commands, such as copy command (cp) and move command (rm), are separate programs that exist in a directory in the file system. For the user, you don't know (or may not care) whether a command is built inside the shell or a separate program.

The shell first checks whether the command is an internal command, and if it is not, then it checks whether it is an application. The application here can be a utility program of Linux itself, such as ls and rm, or a purchased commercial program, such as xv, or public Software (public domain software), just like ghostview. Then the shell tries to find these applications in the search path ($PATH). The search path is a list of directories where executable programs can be found. If the command you type is not an internal command and the executable file is not found in the path, an error message will be displayed. If the command is found successfully, the internal command or application of the shell will be broken down into system calls and passed to the Linux kernel.

Another important feature of the shell is that it is an interpreted programming language. The shell programming language supports most of the program control structures that can be seen in high-level languages, such as loops, functions, variables, and arrays. The shell programming language is easy to learn, and once you master it, it will become your powerful tool. Any command that can be typed at the prompt can also be put into an executable shell program, which means that a certain task can be simply repeated in the shell language.

2. How to start the shell

The shell starts after you successfully log in to the system, and it will always be your means of interacting with the system kernel until you log out of the system. Every user on your system has a default shell. The default shell of each user is specified in the passwd file in the system, and the path of the file is /etc/passwd. The passwd file also contains other things: everyone’s user ID number, a copy of the password encrypted, and the program executed immediately after the user logs in. (Note: In order to enhance security, the current system generally puts the encrypted password in Another file-shadow, and the part of password stored in passwd is replaced by an x ​​character) Although there is no strict requirement that this program must be a Linux shell, it is in most cases.

3. Commonly used shells

There are many different shells available in Linux and UNIX systems. The most commonly used are the Bourne shell (sh) , C shell (csh) , and Korn shell (ksh) . All three shells have their advantages and disadvantages.

  • The author of the Bourne shell is Steven Bourne. It is the shell originally used by UNIX and can be used on every type of UNIX. The Bourne shell is quite good in shell programming, but it is not as good as other shells in handling interaction with users.

  • C shell was written by Bill Joy, and it takes more into account the friendliness of the user interface. It supports some features that the Bourne shell does not support, such as command-line completion. It is generally believed that the programming interface of the C shell is not as good as the Bourne shell, but the C shell is used by many C programmers because the syntax of the C shell is very similar to the C language, which is the origin of the C shell name.

  • Korn shell (ksh) was written by Dave Korn. It combines the advantages of C shell and Bourne shell and is fully compatible with Bourne shell.

In addition to these shells, many other shell programs have absorbed the advantages of these original shell programs and become new shells. Commonly used on Linux are tcsh (extended csh) , Bourne Again shell (extended bash, sh) , and Public Domain Korn shell (extended pdksh, ksh) . Bash is the default shell for most Linux systems.

4. The Bourne Again Shell(bash)

The Bourne Again shell (bash) , as its name implies, is an extension of the Bourne shell. bash is fully backward compatible with the Bourne shell, and many features have been added and enhanced on the basis of the Bourne shell. Bash also contains many advantages of C and Korn shells. Bash has a very flexible and powerful programming interface, as well as a very friendly user interface.

Why use bash instead of sh? The biggest disadvantage of the Bourne shell is that it handles user input. Typing commands in the Bourne shell can be cumbersome, especially when you type many similar commands. And bash has prepared several features to make the input of commands easier.

4.1 Command-Line Completion

Usually when you enter a command in bash (or any other shell), you don't have to enter the command into the full shell to determine the command you want to enter. For example, assume that the current working directory contains the following files and subdirectories:
News/ bin/ games/ mail/ samplefile test/

If you want to enter the test subdirectory, you will enter the following command:
cd test

This command can meet your needs, but bash also provides a slightly different way to accomplish the same thing. Because test is the only subdirectory in the current directory that starts with the letter t, bash can determine what you want to do after you only enter the letter t:
cd t

After you type that letter, the only possibility is test. If you want bash to end the command for you, press the Tab key:
cd t<tab>

When you do this, bash will help you complete the command and display it on the screen. But the command is not executed before you press the enter key, and bash will let you check whether the completed command is what you really need. You may not see its value when you enter a short command like this, even when the command is very short, it will slow down the input speed, but when the command you want to enter is a bit long, you will find how this feature is. Beautiful.

But what happens when there is more than one file beginning with the letter t in the directory? There will be problems when you use the command completion, let us look at the following situation, the current directory has the following content:
News/ bin/ mail/ samplefile test/ tools/ working/

Now there are two files beginning with the letter t in this directory. Suppose you still want to enter the test subdirectory, how to use commands to complete it? If you type as before:
cd t<tab>

Bash will not know which subdirectory you want to enter, because the information given is not unique. If you do, bash will beep to remind you that there is not enough information to complete your command. After the beep, bash does not change the command entered. This will allow you to enter more information on the original basis. In this example, you only need to type an e and press the Tab key again, and then bash will There is enough information to complete your command:
cd test

Whenever you press the Tab key when you enter a command, bash will try its best to complete the command. If it doesn't work, it will beep to remind you that you need more information. You need to type more characters and press Tab again, repeat this process until the command you want appears.

4.2 Wildcard

Another way to make command input easier is to use wildcards in the command. bash supports three wildcards:

  • * Matches any character and any number of characters
  • ? Matches any single character
  • […] matches any single character contained in parentheses

* The use of wildcards is somewhat like command completion. For example, suppose the current directory contains the following files:
News/ bin/ games/ mail/ samplefile test/

If you want to enter the test directory, you will type cd test, or you want to complete it with the command:
cd t<tab>

There is now a third way to do the same thing. Because there is only one file starting with the letter t, you can also use the * wildcard to enter the directory. Type the following command:
cd t*

* Matches any character and any number of characters, so the shell will replace t* with test (the only file in the current directory that matches the wildcard scheme). It will be reliable if there is only one file in the current directory starting with the letter t. But if there is more than one file in the current directory starting with the letter t, the shell will try to enter the first directory that meets the matching scheme. This directory is the first directory in alphabetical order. This directory may or may not be what you expect of.

A more practical use of the wildcard * is to wildcard multiple files with similar names in the command you want to execute. For example, suppose the current directory contains the following files:
ch1.doc ch2.doc ch3.doc chimp config mail/ test/ tools/

If you need to print all .docfiles with the extension , you can use a simplified command like this:
lpr *.doc

In this example, bash will replace *.doc with all files in the current directory whose names match the wildcard scheme. After bash has replaced it, the command will be processed as:
lpr ch1.doc ch2.doc ch3.doc

The lpr command will be called with ch1.doc, ch2.doc, and ch3.doc as parameters.

The wildcard character? Except for matching a single character, other functions are the same as the wildcard character *. If you use the wildcard character? to print all the files with the extension .doc in the aforementioned directory, type the following command:
lpr ch?.doc

The wildcard [...] can match the character or character range given in the brackets. Also take the previous directory as an example, to print all the files with the extension .doc in that directory, you can type one of the following commands:
lpr ch[123].doc
or:
lpr ch[1-3].doc

4.3 Command History

4.4 Input redirection

Input redirection is used to change the input source of a command. Some commands require enough information in the command line to work. For example, rm, you must tell rm the file you want to delete on the command line. Other commands require more detailed input, and the input of these commands may be a file. For example, the command wc counts the number of characters, words, and lines in the files input to it. If you just type wc <enter> on the command line, wc will wait for you to tell it what to count. At this time, bash is like dead, everything you type appears on the screen, but nothing will happen. This is because the wc command is collecting input for itself. If you press Ctrl-D, the result of the wc command will be written on the screen. If you enter a file name as an argument, as in the following example, wc will return the number of characters, words, and lines contained in the file:
wc test
11 2 1

Another way to pass the contents of the test file to the wc command is to redirect the input of wc. The <symbol is used in bash to redirect the input of the current command to a specified file. So you can use the following command to redirect the input of the wc command to the test file:
wc < test
11 2 1
Input redirection is not often used because most commands specify the file name of the input file on the command line in the form of parameters. However, when you use a command that does not accept file names as input parameters, and the required input is in an existing file, you can use input redirection to solve the problem .

4.5 Output redirection

Output redirection is more commonly used than input redirection. Output redirection allows you to redirect the output of a command to a file instead of displaying it on the screen.

This feature can be used in many cases. For example, if the output of a certain command is too much to be fully displayed on the screen, you can redirect it to a file, and then use a text editor to open the file later; also when you want to save the output of a command This method can be used. Also, output redirection can be used when the output of one command is used as the input of another command. (An easier way to use the output of one command as the input of another command is to use pipes. The use of pipes will be introduced in the "Pipe" section of this article)

The use of output redirection is very similar to input redirection, but the symbol for output redirection is >.

Note: The best way to memorize the input/output redirection symbols is to regard <as a funnel, and the small mouth of the funnel points to the commands that need to be input (because the commands that need to be input will be on the left hand side of <), and> as A big mouth points to a funnel with output commands.

As an example of redirection, when you want to save the output of the ls command as a file named directory.out, you can use the following command:
ls > directory.out

4.6 Piping

Pipes can connect a series of commands. This means that the output of the first command will be piped to the second command and used as the input of the second command, the output of the second command will be used as the input of the third command , and so on. The output will be the last command in the pipe line is displayed on the screen (if the command line using the output redirection, it will put a file).

You can create a pipe line by using the pipe character |. The following example is a pipe line:
cat sample.text | grep "High" | wc -l
this pipe will send the output of the cat command (list the contents of a file) to the grep command. The grep command looks for the word High in the input. The output of the grep command is all lines containing the word High. This output is sent to the wc command. The wc command with the -l option will count the number of lines in the input. Suppose the content of sample.txt is as follows:

Things to do today:
Low: Go grocery shopping
High: Return movie
High: Clear level 3 in Alien vs. Predator
Medium: Pick up clothes from dry cleaner

The pipeline will return result 2, indicating that you have two very important things to do today:
cat sample.text | grep "High" | wc -l
2

4.7 Prompt

Bash has two levels of user prompts. The first level is the prompt you often see when bash is waiting for command input. The default first-level prompt is the character $ (if it is a super user, it is the # sign). You can change your default prompt by changing the value of the PS1 variable in bash, for example:
PS1="Please enter a command"

Change the prompt of the bash shell to the specified string.

Displays a second-level prompt when bash expects to enter more information to complete the command. The default second-level prompt is >. If you want to change the second-level prompt, you can do this by setting the value of the PS2 variable:
PS2="I need more information"

In addition, you can also use special characters to define your prompt. The following list lists the most commonly used special characters.

character meaning
/! Display the history record number of the command.
/# Display the command number of the current command.
/$ Display the $ sign as a prompt, if the user is root, the # sign is displayed.
// Display backslashes.
/d Display the current date.
/h Display the host name.
/n Print a new line.
/nnn Display the octal value of nnn.
/s Display the name of the currently running shell.
/t Display the current time.
/ u Display the user name of the current user.
/W Display the name of the current working directory.
/w Display the path of the current working directory.

These special characters can be combined into many useful prompt schemes (or they can be combined into very strange schemes). For example, set PS1 to:
PS1="/t"

This causes the prompt to display the current time as shown below (there will be no spaces after the prompt):
02:16:15

And the following settings:
PS1=/t

Will cause the prompt to become the following:
t

This shows the importance of quotation marks in the settings, the following prompt string:
PS1="/t// "

Will make the prompt look like this:
02:16:30/

In this case, there will be a space after the prompt because there is a space in the quotes.

4.8 Job control

Job control can control the behavior of the currently running process. In particular, you can suspend a running process and resume its operation later. Bash keeps track of all started processes. You can suspend or resume a running process at any time during its lifetime.

Press Ctrl-Z to suspend a running process. The bg command makes a suspended process resume running in the background, while the fg command makes the process resume running in the foreground. These commands are often used when the user wants to run in the background and accidentally put it in the foreground. When a command is run in the foreground, it will prohibit the user from interacting with the shell until the end of the command. This usually causes no trouble, because most commands are executed quickly. If the command you want to run takes a long time, we usually put it in the background so that we can continue to enter other commands in the foreground. For example, if you enter this command:
command find / -name "test" > find.out

It will look for a file named test in the entire file system and save the result in a file named fing.out. If running in the foreground, depending on the size of the file system, your shell will be unavailable for several seconds or even several minutes. If you don't want this, you can enter the following:
control-z
bg

The find command is suspended first, then continues to be executed in the background, and you can immediately return to bash.

Guess you like

Origin blog.csdn.net/mahoon411/article/details/112499963