Bash Shell Summary

Source: Internet
Author: User
Tags arithmetic set time


Mans Bash


1. How the Shell works (1)


Kernel <---> Shell <---> Users

Parent Process Fork---wait

Child process Exec--exit

Related Man documents:

Fork (2)

Clone (2)

Execve (2)

Exit_group (2)

EXIT4 (2)

Strace (1)-->strace-e


2. Shell Syntax (2)

1. Simple command (kw:simple Commands)

Basic form: command [ARG] ...

Example:

Cd

Ls-l

Cp/data/log/backup/log


2. Piping (Kw:pipelines)

Basic form: CMD1 | CMD2 | CMD3 + pipe + small command = complex command

Standard output of the left command as standard input to the right command

Using |& instead of |, you can pass the standard error along to the right.

Each command in the pipeline executes in a child process

Commands in the pipeline run at the same time

The return state of the pipeline is the return state of the command at the far right of the pipeline

Example:

Ls/students|wc-l

IP a| Grep-w inet


3. List (kw:lists)

A series of pipelines is the list

The matches used for split pipelines in the list are:

;: command executes from left to right sequentially

&: Command executes simultaneously

&&: The left side succeeds in executing the right side.

| |: Left failure to execute right

The end of the list can have; & If it is &, the right-most pipe is placed in the background execution


4. Compound command (Kw:compound Commands)

List is executed as a whole in a child process

{list;} The list is executed as a whole in the current process

((expression)) arithmetic operations

((n++))

((total= $first + $last))

[[expression]] returns 0 if the expression is true, otherwise returns non 0, supports regular

If, case, for, and while these are the shell's Process control directives


3. References (1), (kw:quoting)

The reference symbol in the shell is used to remove the special meaning of special characters

1. Double quotes cannot escape $

2. Single quotes escape all conformity, except ' itself

3. Backslash single character


4. Parameters (2), (kw:parameters)

The Shell parameter is the "thing" that holds "value", which can be a name, a number, or some special symbol.

1. Variables

2. Position parameters (Kw:positional Parameters)

3. Special parameters (Kw:special Parameters)

$*: A string of all positional parameters merged into

[Email protected]: All positional parameters, if placed in double quotes, save the bounds of the parameter

4. Shell built-in variables (Kw:shell Variables)

5. Array (kw:arrays)

A= (E1 E2 E3)

Declare-a A

A[x]=1

a[y]=2

${name[0]} <--gets the No. 0 element

${#name [*]} <--The length of the array

${!NAME[*]} The keys of the <--array

${name[*]} values of the <--array

unset name <--Delete entire array

unset name[0] <--Delete an element in the array


5. Expand (2), (kw:expansion)

The shell performs a series of operations on the command line string that is read before executing the command, and common deployment operations are:

1. Curly brace Expansion (Kw:brace Expansion)

{1..10}, {1..10..2}, {A.. Z

d{1,2,3}, D1 D2 d3


$ cp-v/tmp/a/b/c/deep{,2}

'/tmp/a/b/c/deep ', '/TMP/A/B/C/DEEP2 '


2. Wave Number expansion (Kw:tilde Expansion)

~: Home directory of the current user

~user: Specify the user's home directory

~+: Current Working directory

~-: Previous working directory


3. Parameter expansion (Kw:parameter Expansion)

$, ${name}

${name:-default} <--Default value

${name:offset} <--Intercept string

${name:offset:length} <--intercept a certain length of string

${#name} <--Calculating string length

${name#word} <--Delete string header

${name%word} <--Delete string trailing

${name/pattern/string} <--String substitution

${name^pattern} <--converted to uppercase

${name,pattern} <--converted to lowercase


4. Command replacement (Kw:command Substitution)

$ (cmd list) <--should currently use this

' cmd list ' <--obsolete syntax


5. Arithmetic expansion (kw:arithmetic Expansion)

$ (expression) <--should currently use this

$[expression] <--obsolete syntax


6. Process substitution (kw:process Substitution)

< (cmd list)

> (cmd list)


7. Path name expansion (Kw:pathname Expansion)

*, ?, []

Expand, expand, not unfold, and remain intact.


6. Redirection (1.5), (kw:redirection)

Before a command executes, its inputs and outputs may be redirected by the shell, and the redirection operation has the following functions:

redirect Input (kw:redirecting input, here Documents, here Strings)

Wc-l < file

CMD <<eof

..

Eof

CMD <<< "string"

Redirected outputs (kw:redirecting output)

Copy file descriptor (kw:duplicating descriptors)

EXEC 4>&3 <--to copy 3 into 4.

Move file descriptor (kw:moving descriptors)

EXEC 4>&3-<--move 3 to 4

Close File descriptor

EXEC 3>&-<--off 3

Open file Descriptor read-write (kw:opening descriptors for Reading and Writing)

EXEC 3<>file <--Open in read-write form


0 stdin

1 stdout

2 stderr

7. Arithmetic Operations (0.5)

Bash Shell supports integer arithmetic operations

1. ((expression)) (Kw:arithmetic EVALUATION)

2. $ (expression)) (Kw:arithmetic Expansion)

3. Decimal operations

BC, Python, awk

8. Deployment of simple commands (0.5), (kw:simple command EXPANSION)

When the shell executes a simple command, it does the following before it actually executes:

1. Remove the variable assignment and redirect Section

2. Proceed to the rest of the section

3. Performing a redirect operation

4. Perform variable assignment operations

After the simple command is expanded, if there is a command name in the expanded result, the process goes to the execution of the command stage.


9. Execution of the order (1), (Kw:command execution)

1. If the command name does not contain a slash, the command is searched sequentially:

1. Functions

2. Internal commands

3. Search for Directories in path

2. If the search succeeds, or if the command name contains a slash, start execution.

3. If the search fails or the program file does not exist, 127 is returned.


10. Execution Environment of the command (0.5), (Kw:command execution Environment)

1. Open the file

2. Current working directory

3. Umask

4. Signal Trap--process communication

5. Shell variables

6. Shell functions

7. Shell's function options (set, shopt) Set-o Vim/emacs

8. Shell Name

9. Various process IDs ($$, $PPID)


11. Environment variables (0.5), (kw:environment)

When a command is executed, a string array (list) is passed to it, which is called "Environment" or "environment variable".

The environment variables of the shell can be passed to the child process. To set the shell's environment variable command:

1. Export

Export time <--set time to environment variable

Export-n time <--time programming non-environment variables

2. Declare

Declare-x time <--set time to environment variable

Declare +x time <--make time programming non-environment variables


Shell internal Commands (4.5), (Kw:shell BUILTIN COMMANDS)

The shell's internal commands are executed by default in the current process, often with

01.: Empty Command

A.., source

The point command (source) reads the code inside the file into the "current process" execution.

Break, continue.

Break <--End the entire loop

Continue <--End the current loop and go to the next cycle

CD, PWD

CD <--must be an internal command

Declare, export, ReadOnly, local

Set, unset, Shopt-"Set change the shell's switch

. Echo, read, printf

. exec

. Exit, return

Ten. Kill, Trap

One. Shift

Test, [

Wait


Bash Shell Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.