What is a shell

Simple point of understanding, is the system and computer hardware interaction used in the medium, it is only a tool of the system. In fact, there’s another layer between the shell and the hardware: the kernel. For example, if the computer hardware is compared to a person’s body, and the system core is the human brain, as for the shell, it seems more appropriate to compare it to the human facial features. Back to the computer, what the user directly faces is not the computer hardware but the shell. The user tells the shell the instructions, and then the shell transmits them to the system kernel, and then the kernel dictates the computer hardware to perform various operations.

Bash

There are two main shell classes in Unix:

  1. Bourne shell − If you are using a Bourne type shell, the $character is the default prompt.
  2. C shell − If you are using a C-type shell, the % character is the default prompt.

One of the Linux distributions — Redhat/CentOS — the shell installed by default is called bash, or Bourne Again Shell, which is an enhanced version of sh (Bourne shell). Bourn Shell is one of the earliest shells to come into use. The founder is Steven Bourne, so it is called Bourn Shell in honor of him. So what’s special about bash?

  1. Record command history Linux keeps a record of all the commands we type. By default, it can record up to 1000 command history. These commands are stored in the.bash_history file in the user’s home directory.
  2. Command and File name completion Press TAB to complete a command, a path or file name. Press TAB twice in a row and the system lists all commands or file names.
  3. Alias, which was introduced earlier, is one of bash’s unique features. We can use alias to alias a frequently used and long instruction to a simple and easy to remember instruction. Under bash, you can use * to match zero or more characters, instead of? Matches a character.
  4. Input/output redirection from input redirection is used to change the input of a command, and output redirection is used to change the output of a command. Output redirection is more common and is often used to enter the result of a command into a file rather than on the screen. Input redirection command is <, output redirection command is >, error redirection 2>, and append redirection >>.
  5. Pipeline operators have been celebrated in front of the pipe “|”, that the results of the previous command is lost to the back of the command.
  6. Job control. When running a process, you can pause it (Ctrl+ Z), then resume it with fg, run it in the background with bg, or terminate it (Ctrl+ C).

How does Bash parse commands

  1. Read the command line by line

  2. The characters inside double quotation marks will lose their original meaning, except for $, “and \. Characters inside single quotes will lose their original meaning, including $, “and \.

  3. Sets the input line of string as; Split into multiple commands.

  4. Handle special characters {.. }, < (..) , <… , < < <.. . |.. Such special characters are processed in a special execution order. Redirection symbols are removed from the command line, so when executing a command… > log, 2>&1 are commands that are not submitted to the command processing process in the kernel. Other symbols are replaced by their corresponding result expressions, such as {.. } commands:

$ echo{a.. c} a b cCopy the code
  1. Variable substitution will be a variable with the $sign$parameterI’m going to replace it with variable content, and the terminology is calledParameter Expansion.
$ echo $PWD
/z/ros
Copy the code
  1. The principle for dividing the command line into executed commands and arguments is that any whitespace (Spaces, tabs) is used as a separator to separate the entire command into words. The first word of the split result is used as a command and the other words are used as parameters. If the command word contains blank space, enclose it in quotation marks.
$ My Command /foo/bar   ## This will execute the command named 'My' because it is the first word.
$ "My Command" /foo/bar ## This will execute the command named 'My Command' because the space inside the quotes has lost its special meaning allowing it to split words.
Copy the code

When copying files from Windows or MacOS to Linux, some file names may contain special characters that Are not supported by Linux, such as Spaces, #, etc. The file name file 1127.txt is legal on Windows, but not on Linux.

$ rm file 1127.txt // not work 
$ rm "file 1127.txt"  // work
Copy the code
  1. Execute command a. If the command is function or builtin, it will be processed by the same Bash process that received the command. B. Otherwise (such as hadoop FS commands), Bash will fork off, create a new Bash child, pass the parsed command to it, and wait for it to return the result. In general, the child will inherit the parent’s standard stream.

Ref:

  1. Learn the basics before shell scripting
  2. The Bash Parser
  3. Remove files with names containing strange characters such as spaces, semicolons, and backslashes in Unix