Shell Simple summary--symbol

Source: Internet
Author: User
Tags arithmetic shebang

First, Shell introduction

The role of the shell is to interpret the command that executes the user, and the user enters a command that the shell interprets as an "interactive" line. Another way to execute a command is "batch", where the user writes a shell script that has many commands and the shell executes the commands at once. The shell program reads and executes the commands from a single line in the script, which is equivalent to a user knocking a line of commands from the script to the shell prompt for execution. Shell scripts are interpreted and do not need to be compiled.

1. What is Shell execution script

The suffix of the script is generally. sh

Scripts typically start with #!, called "Shebang", followed by an interpreter

The script comment starts with #

Need to change permissions when executing: chmod +x

* The script is actually a file that can execute multiple lines of command.

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/A7/43/wKioL1nkWq_x75M8AAB88KHqGsA882.png "title=" Shell.png "alt=" Wkiol1nkwq_x75m8aab88khqgsa882.png "/>

The first line of the shell script should never be written wrong: #!/bin/bash.

2. Shell script Execution Process

The shell will fork a child process and call exec to execute./test.sh This program, the exec system calls the code snippet of the process to replace it with the shell script and executes it from its _start. But this script. sh file is a script file, there is no code snippet and _start function at all. So exec executes another mechanism.

Since the first line specifies the interpreter with Shebang, the current process is replaced with the Interpreter program code, and the text file is passed to the interpreter as a command-line argument starting with the _start of the interpreter.

* Interpreted language requires interpreter interpretation only, no compiler required, such as Shell scripting language; C language is a compiled language

Second, shell variables

1. Environment variables

Environment variables can be passed from the parent process to the child process, so the environment variables of the shell process can be passed from the current shell process to the forked sub-process, using PRINTENV to display the environment variables of the current shell process.

The *fork function gets a child process that inherits the entire process's address space from the parent process, including: process context, process stack, memory information, open file descriptor, signal control settings, process priority, process group number, current working directory, root directory. Resource constraints, control terminals, environment variables, etc. However, the lock child process set by the parent process does not inherit and the pending signal set of the child process is set to the empty.

2. Local Variables

Only the current shell process exists, with the SET command to display all variables defined in the current shell process, including local and environment variables.

When you define an environment variable, there can be no spaces on either side of the equal sign, or it will be interpreted by the shell as command and command-line arguments. A variable defines a backward existence of the current shell process, which is a local variable, and export allows local variables to be exported as environment variables.

Use unset to delete defined environment variables

Varname=value #定义本地变量

Export Varname=value #导出本地变量, can also be written as Varname=value;

Export VARNAME

unset VARNAME #删除已定义的环境变量或本地变量

3. Variable reference

If a variable is called VARNAME, use ${varname} to represent its value, and without causing ambiguity
You can use $varname to represent its value. The differences between the two representations are compared by the following example:
Echo $SHELL
Echo $SHELLABC
Contrast:
Echo ${shell}
Echo ${SHELL}ABC
Note that when you define a variable, you do not use $ when you take the variable value. Unlike the C language, shell variables do not need to be explicitly defined types, in fact the values of shell variables are strings, such as we define VAR=45, in fact, the value of Var is a string 45 instead of an integer. The shell variable does not need to be defined before it is used, and the value is an empty string if a variable is not defined.

Third, replace

1. File name substitution

  these characters for matching are called wildcards ( Wildcard), as follows:
wildcard *: match 0 or more any character
?: Match a any character
[several characters ]: Match party $ ls/dev/ttys*
$ ls ch0?. DOC
$ ls ch0[0-2].doc
$ ls ch[012][0-9].doc
> Note that globbing matches the file name that was opened by the Shell, That is, before the parameters passed to the program has been expanded , such as the above LS ch0[012].doc command, if the current directory has Ch00.doc and Ch02.doc, then the parameter passed to the LS command is actually the two file name, rather than a matching string.

2. Command-line substitution

It is also a command that is enclosed in anti-quotes, which the shell executes first, and then immediately substitutions the output to the current command line.
$ Date= ' DATE '
$ echo $DATE
For example, define a variable to hold the output of the date command:
Command substitution can also be represented by $ (): $ date=$ (DATE)
Arithmetic substitution: $ (())
For arithmetic calculations, the value of the shell variable in $ (()) is converted to an integer, for example:
$ var=45
$ echo $ (($VAR +3))
Only the +-*/and () operators can be used in $ (()), and only integer operations can be done.

Four, escape character \

The \ Backslash is used as an escape character to remove the special meaning of a single character immediately following it.

Cases:

Touch $ $ #创建一个名为 $ of File

Touch \$\ \$ #创建一个名为 $ $ file (with spaces in the middle)

There is also a character that does not have a special meaning, but it is cumbersome to use it as a filename. If you want to create a file with a filename that begins with the-number, this is not possible: even adding \ Escaping is an error: because the various UNIX commands use the command-line arguments that begin with the-number as an option for the command, not as a file name. If you do not want to process a file name that begins with a-number, there are two ways to do this:

touch./-hello #前面加上当前路径

Touch---File #前面加上两个--

Five, single and double quotation marks

1. Single quotation mark

Unlike the C language, the single and double quotes in shell scripts are the same as the delimiter of the string, not the delimiter of the character. single quotation marks are used to hold the literal value of all characters within the quotation marks, even if the \ and carriage returns within the quotation marks are no exception, but single quotes cannot appear in the string. If the quotation marks are entered without pairing, the shell gives a continuation prompt that asks the user to match the quotation marks.

2. Double quotation marks

Double quotation marks are used to keep the literal value of all characters in quotation marks (carriage return is no exception), except in the following cases:

$ plus variable name to take the value of the variable
Anti-quotation marks still indicate command substitution
\$ represents the literal value of $
The literal value of \ ' representation '
The literal value of \ "represents"
\ \ denotes the literal value of \ In addition to the above, there is no special meaning in front of other characters, only the literal value

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/08/99/wKiom1nkfNbBdkNgAAAtJPw0Jlc200.png "title=" Shell1.png "alt=" Wkiom1nkfnbbdkngaaatjpw0jlc200.png "/>

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/08/99/wKiom1nkfNaQeKNdAAAksYdiFbE960.png "title=" Shell2.png "alt=" Wkiom1nkfnaqekndaaaksydifbe960.png "/>


Shell Simple summary--symbol

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.