Chapter 10: Understanding and learning bash

First, the hardware core, Shell

The operating system is a set of software, hardware and management systems to control the entire activity detection. The operating system can not be free to operate user, if used improperly, it will cause the system to crash. But we always need to let the user's operating system, so there is an application. User command center through the application, so that the core task of reaching the hardware we need. So the application (shell) as eggs (kernel) of the same shell.

  • Shell program functions merely provide a user interface to the operating system, narrow: software instruction sequence aspects, such as the bash; broad: the graphics interface software

Second, why should learn the text shell interface

shell as an application, there are many versions, shells / etc / shells file is used
[Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-E1Rd2Rfi-1582378802868) (https://user-images.githubusercontent.com/56629574/67351116-417eee00-f57f-11e9-8a5a-b00c6ace12eb.png)]
the user is the default shell / etc / passwd in
1571797982(1)

Third, variable

1. The use of a variable

  • Access to the variables: echo $ variable
  • Setting variables: variable name = variable value
  • Cancellation variable: unset variable name
  • In the bash environment, undefined variable names, echo display is empty

Application: for example, often go to a working directory, / a / b / c / d /
can work = "/ a / b / c / d"
each time you enter the work with cd $ work
View 2. Variable

  • View the environment variables: env
  • View the environment variables and custom variables: set
  • Custom variable into an environment variable: export variable name
  • Environment variable into custom variables: declare -x variable names
    Why do you want to convert environmental variables to customize the variables?
    1574047211(1)
    Because bash is a shell (application program interface), this instruction is issued bash bash this derivative, i.e. subroutines. If the implementation of sub-bash, bash the sub-environment interface is the environment, the original bash suspended. If you want to return to the parent program, will have to bash out the end of the second (issued exit or logout)
    subroutine can only inherit the environment variables of the parent program, the subroutine will not inherit the parent program of a custom variable, so in the original bash
    custom variables in the sub-bash will disappear, leaving the road has been returned to the parent subroutine program, these custom variables to appear.

3. Variable keyboard read, and declare an array
variable can be set in a predetermined manner. 2, the keyboard embodiment used may be read to set the variable

  • Keyboard read: read [-pt] variable name
    variable is the default string type, if you want another type (such as arrays, value), will have to declare variables
  • Declare the variable type: declare [-aixr] variable name

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (https://user-images.githubusercontent.com/56629574/69023567-2d050880-09fa (img-GhmyfWjv-1582378802875) -11ea-88ee-047062981e66.png)]

4. Remove the contents of variables, substituted and replaced

Fourth, command aliases and command history

1. command aliases set: Alias, unalias
Alias LM = 'LS -al | More'
benefits: ① simplify the command length ② to prevent accidental deletion operations such as alias rm = 'rm -i' you can delete every time, have prompted Do you want to delete, delete the file rather than directly.

The difference between variable and command aliases : alias command is a command, can be used directly; variables need to use echo
2. command search order

  • A relative / absolute path to execute the command
  • By the alias locate the command to execute
  • Performed by a built-in bash (BUILTIN) instruction
  • Search through the order to $ PATH variable of the first instruction to execute
    Here Insert Picture Description
    so different with ls and / bin / ls results of the implementation case

Fifth, data traffic redirection

After the original command output content on a screen, or output to a file apparatus
** 1 standard output: code 0 **> or >> differences:> will overwrite the original file content
originally output screen, the output to a file
such as: LS >> a.txt
2. standard error: code 1,> or >>

3. Standard Input: <<< << or keyboard is designated terminator
originally entered at the keyboard, the input file (file must be)
CAT> b.txt <a.txt
keyboard:
1574064087(1)
Analyzing 4. command execution based on:; && ||

  • Without considering the relevance of command cmd; cmd (regardless of whether the command is executed successfully)
  • Consider the correlation && command: right in front of the Executive, will be performed later
  • Consider command relevance ||: short circuit before the implementation of the principles of success, after not executed; the former error, before execution
    , for example: to test ls / a / b exists, if there is "exist" is displayed; if not, then the display "not exist"
    1574065483(1)

Sixth, the command pipeline (pipe)

Pipeline and the pipeline is different from the above-described V.4 command is output before a next command is input; does not exist in the interaction between the command V.4
[image dump outer link fails, the source station may have security chain mechanism, it is recommended save the picture down directly upload (img-EvMT4Pqz-1582378802881) ( https://user-images.githubusercontent.com/56629574/69036922-c562b380-0a21-11ea-8c30-38fafae76a91.png)]

Published 550 original articles · won praise 88 · views 260 000 +

Guess you like

Origin blog.csdn.net/jiangshangchunjiezi/article/details/104451213