A concise summary of special symbols and meanings in the Linux shell (contains a large majority) _linux shell

Source: Internet
Author: User
Tags arithmetic

There are a lot of special symbols in the Linux shell, which we should pay special attention to when we write Shell script: On the one hand, we need to know the usage of these special symbols, these symbols can be used to achieve a multiplier effect;

These special symbols are listed roughly as follows:

Copy Code code as follows:
# ; ;; . ,//' String ' |! $ ${} $? $$ $* "String" * * * *? : ^ $# $@ ' command ' {} [] [[]]] (()) | | && {Xx,yy,zz,...} ~ ~+ ~-&/+–%= = =!=

Output/Input Redirect:

Copy Code code as follows:
> >> < <<: > &> 2&> 2<>>& >&2

Here is a description of the meaning and use of these special symbols ...

# Well No. (comments)
It's almost a sign that's all over the field.
#!/bin/bash
Well numbers often appear at the beginning of a line, or after a complete instruction, which indicates that the symbol is followed by the callout text and is not executed.
# This is comments.
echo "a = $a" # a = 0
Because of this feature, when you temporarily do not want to execute a line of instructions, simply add a # at the beginning of the line. This is commonly used in the writing process.
#echo "a = $a" # a = 0
If used in a directive, or in quotation marks or in double quotes, or behind a slash, he becomes a general symbol without the special features described above.

~ Account's Home directory
is a common symbol, representing the user's home directory: cd ~; You can also add the name of an account directly after the symbol: CD ~user
Or as part of a path: ~/bin;~+ the current working directory, which represents the current working directory, and she has the same effect as the built-in instruction pwd.
# echo ~+/var/log
~-Last working directory, this symbol represents the last working directory.
# echo ~-/etc/httpd/logs

; Semicolon (Command separator)
in the shell, the symbol that acts as a "continuous instruction" function is "semicolon." For example, the following examples: CD ~/backup; mkdir startup; CP ~/.* startup/.

;; Consecutive semicolon (Terminator)
dedicated in the case of the option to assume the role of Terminator.
Case "$FOP" Inhelp) echo "usage:command-help-version filename"; Version) echo "version 0.1″;; Esac

. Comma (dot)
in the shell, users should be aware that a dot represents the current directory and that two dot represents the upper directory.
Cdpath=.:~:/home:/home/web:/var:/usr/local
In the uplink cdpath setting, the dot after the equals sign means the current directory.
If the file name starts with Dot, the file is a special file, and the LS directive must be added with the-a option to be displayed. In addition, in regular expression, a dot represents matching a character.

' String ' single quotation mark (quote)
the enclosed quotation marks are treated as a single string. Within the quotation marks represents the variable's $ symbol, which has no effect, that is, he is treated as a generic symbol to prevent any variable substitution.
Heyyou=homeecho ' $heyyou ' # We get $heyyou

"string" double quotation mark (double quote)
content that is enclosed in double quotes will be treated as a single string. It prevents wildcard extensions, but allows variable extensions. This differs from the way a single argument is handled.
Heyyou=homeecho "$heyyou" # We get Home
' Command ' inverted quotation mark (backticks)
In front of the single double quotes, the string is enclosed, but what if the string is a column of command columns? The answer is not to execute. To handle this situation, we have to do it in inverted single quotes.
fdv= ' Date +%f ' echo ' Today $FDV '
The date +%f in the inverted quotation mark is treated as an instruction, and the result of execution is brought into the FDV variable.

, comma (comma)
This symbol is often used in the operation as a "partition" use. The following example
#!/bin/bashlet "T1 = ((A = 5 + 3, B = 7–1, c = 15/3))" echo "T1 = $t 1, a = $a, B = $b"

/slash (forward slash)
represents a directory when the path is represented.
CD/ETC/RC.DCD.. /.. CD/
Usually a single/representative root directory meaning; in arithmetic, a symbol for division.
Let "NUM1 = ((a = 10/2, B = 25/5))"

\ slash (Escape)
in the interactive mode of the escape character, there are several functions, before the instruction, there is a cancellation of aliases function, placed in the special symbol before the role of the special symbol disappeared; at the very end of the instruction, the instruction connects the next line.
# type RM
RM is aliased to ' rm-i '
# \RM \*.log.
In the example above, I added an escape character before the RM instruction to temporarily remove the alias and restore the RM directive.
# Bkdir=/home
# echo "Backup dir, \ $bkdir = $bkdir"
Backup dir, $bkdir =/home
In the above example, the \ $bkdir in Echo, Escape will cancel the function of the $ variable, so output $bkdir, and the second $bkdir will output the contents of the variable/home.

| Pipe (Pipeline)
pipeline is a UNIX system, a fundamental and important concept. Link the standard output of the previous instruction to the standard input for the next instruction.
who | Wc-l
The use of this concept is quite helpful in streamlining script.

! Exclamation point (negate or reverse)
usually it represents the function of the inverse logic, for example, in conditional detection, using!= to represent "not equal"
If ["$?"!= 0]thenecho "executes error" Exit 1fi
She plays the role of "anti-logic" in regular expressions
LS A[!0-9]
In the example above, the representative shows other documents in addition to A0, A1 ... A9 these several documents.

: Colon
in bash, this is a built-in instruction: "Do nothing", but return a status value of 0.
:
echo $? # response to 0
: > f.$$
The above line is equivalent to Cat/dev/null > f.$$. Not only is the writing brief, but also the execution efficiency is many.
Sometimes, the following types of usage can also occur
: ${hostname?} ${user?} ${mail?}
The purpose of this line is to check that these environment variables are set and that no settings will display the error message as a standard error. Such checks, if used like test or if, can be handled basically, but not as simple and efficient as the example above.
In addition to the above, there is a place that must use a colon
Path= $PATH: $HOME/fbin: $HOME/fperl:/usr/local/mozilla
In the user's own home directory, in the. bash_profile or any feature-like file, in the context of "path", we all use colons to do the partition.

? Question mark (wild card)
the role played on the filename extension (filename expansion) matches an arbitrary character, but does not contain a null character.
# ls A?A1
Use her features to make more precise file name matching.

* asterisk (wild card)
a fairly common symbol. On the filename extension (filename expansion), she is used to represent any character, including null characters.
# ls a*a A1 Access_log
In an operation, it represents "multiplication."
Let "Fmult=2*3″
In addition to the built-in instruction let, there is also an instruction on the operation of expr, where the asterisk also acts as a "multiplication" role. But be careful in use, he must be preceded by an escape character.

* * Sub-square operation
the two asterisks represent the meaning of the "square" at the time of operation.
Let "sus=2**3″echo" sus = $sus "# sus = 8

$ Money Number (dollar sign)
variable substitution (Variable substitution) for the representative symbol.
Vrs=123echo "VRS = $vrs" # VRS = 123
In addition, the Regular Expressions is defined as the end of the line (End-of-line). This is commonly used in grep, SED, awk, and Vim (vi).

The regular expression
bash for the

${} variable defines a number of uses for ${}. The following is a table column
${parameter:-word} ${parameter:=word} ${parameter:?word} ${parameter:+word} ${parameter:offset that is taken from the line description Parameter:offset:length} ${!prefix*} ${#parameter} ${parameter#word} ${parameter# #word} ${parameter%word} ${ Parameter%%word} ${parameter/pattern/string} ${parameter//pattern/string}

$* referencing the script's execution reference variable. The algorithm for referencing the parameter is the same as the general instruction, the instruction itself is 0, followed by 1, and so on. Reference variables are represented in the following ways:
$, $, $, $, $, $, $, $, $, $, ${10}, ${11} ...
Single-digit, you can use numbers directly, but more than two digits, you must use the {} notation to enclose. The
$* is the symbol representing all reference variables. When used, it is necessary to enclose the case with double quotes.
echo "$*"
also has a symbol that has the same effect as $*, but the utility is slightly different from how it is handled. The

$@
$@ has the same symbolic effect as $*, but they have a different point. The
Symbol $* treats all reference variables as a whole. But the symbol $@ still retains the section idea of each reference variable.
$#
This is also a symbol associated with a reference variable, and her role is to tell you the total number of reference variables.
echo "$#"

$? status variable
in general, the process of a UNIX (Linux) system ends by executing the system call exit (). The return value is the status value. Passed back to the parent process to check the execution status of the child process.
If the normal instruction program succeeds, its return value is 0; failure is 1.
Tar cvfz dfbackup.tar.gz/home/user >/dev/nullecho "$?" $$
Because the ID of the process is unique, it is not possible to have a repetitive PID at the same time. Sometimes the script will need to generate temporary files to store the necessary data. This script may also be used by users at the same time. In this case, the fixed file name is not reliable in the wording. Only dynamic file names can be generated to meet the needs. Symbolic $$ may meet this requirement. It represents the current Shell's PID.
Echo $HOSTNAME, $USER, $MAIL "> ftmp.$$
uses it as part of the file name to avoid overwriting the same file name at the same time.
PS: Basically, the system recycles the completed PID and then assigns it again as needed. So script even if the temporary file is the use of dynamic filename, if the script is finished after the execution is still not clear, can cause other problems. The
() instruction Group (command Group)
encloses a sequence of consecutive instructions in parentheses, which is called the instruction Group for the shell. As in the following example: (CD ~; vcgh= ' pwd '; echo $vcgh), the instruction group has an attribute in which the shell executes the set of instructions by generating Subshell. Therefore, the variables defined therein are used only for the instruction group itself. Let's take a look at the example
# cat Ftmp-01#!/bin/basha=fsh (A=INCG; echo-e "/n $a/n") echo $a #./FTMP-01INCGFSH
In addition to the instruction group above, parentheses are also used in array The definition of a variable, and it is also applied to other situations where you may need to add an escape character to use, such as an expression.

The role of the

(())
is similar to the let instruction, and is used for the built-in function of bash for arithmetic operations. Therefore, it is much better to perform efficiently than using let directives.
#!/bin/bash ((a =)) echo-e "inital value, a = $a/n" ((a++)) echo "After a++, a = $a"

{} curly braces (blo CK of code)
sometimes appears in the script, with a paragraph or paragraphs in curly braces that end with a semicolon or a set of variables.
# cat FTMP-02#!/BIN/BASHA=FSH{A=INBC echo-e "/n $a/n"}echo $a #./FTMP-02INBCINBC
This usage is very similar to the instruction group described above, but there is a different point where it The current shell execution does not produce subshell. The
curly braces are also used in functions of functions. Broadly speaking, simply using curly braces, the function is like a function without a specified name. So writing a script is a pretty good thing to do. This approach, especially for the redirection of output inputs, can streamline the complexity of the script.
In addition, curly braces have another use, as follows
{Xx,yy,zz,...}
This combination of curly braces, used in the combination of strings, see example
mkdir {usera,userb,userc}-{home,bin,data}
We get Usera-home, Usera-bin, Usera-data, Userb-home, Userb-bin, Userb-data, Userc-home, Userc-bin, Userc-data, these several directories. This group of symbols is quite extensive in applicability. If you can use it wisely, the rewards are streamlined and efficient. Like the following example
Chown Root/usr/{ucb/{ex,edit},lib/{ex?.? *,HOW_EX}
If it is not because of support for this usage, we have to write several lines to repeat several times!

[] Bracket
often appear in the process control, play the role of the judge-style. If ["$?"!= 0]thenecho "executes error" Exit 1fi
This symbol holds a role similar to "Scope" or "collection" in a regular expression
Rm-r 200[1234]
In the example above, the Representative deletes 2001, 2002, 2003, 2004, and so on.

[[]]
This set of symbols is essentially the same as the previous [] symbol, but she allows direct use of the and && logic and other symbols.
#!/bin/bashread Akif [[$ak > 5 | | $ak < 9]]thenecho $AKFI | | This is often seen in logical notation, representing symbols of or logic. && logical Symbols This is also often seen, representing the symbols of and logic. & Background work a single & symbol, placed at the end of the complete instruction column, means that the instruction column is placed in the background to work. Tar cvfz data.tar.gz data >/dev/null &
/Word boundaries
This set of symbols is defined as the meaning of "boundary" in regular expressions. For example, when we want to find the word, if we use
grep the Filea
You will find that words like there are also used as matching words. Because it happened to be part of there. If we're going to have to avoid this, we have to add the symbol "boundary".
grep '/' Filea

+ PLUS sign (plus)
in an expression, she is used to denote "addition."
Expr 1 + 2 + 3
In addition, in regular expressions, the meaning of the front character of "many" is used.
# grep ' 10/+9′fileb109100910000910000931010009# This symbol must be preceded by an escape character when used.

-Minus sign (dash)
in an expression, she is used to denote "subtraction."
Expr 10–2
It is also an option symbol for system directives.
ls-expr 10–2
In the GNU directive, the meaning of "standard input" is represented if the-symbol is used alone, without any name of the file added. This is a common option for the GNU directive. such as the following example
Tar XPVF-
Here the-symbol, both represents reading data from the standard input.
However, in the CD directive is more special
CD-
This represents changing the working directory to the previous working directory.

% Division (modulo)
in an expression, used to represent "division."
Expr 10% 2
In addition, it is also applied to the following in the regular expression of a variable
${parameter%word}${parameter%%word}
A% represents the shortest word match and two of the longest word matches.

= equals sign (equals)
a symbol that is often seen when a variable is set.
Vara=123echo "Vara = $vara"
Or a PATH setting, or even applied to such uses as arithmetic or judgment.

= = equals sign (equals)
often seen in conditional judgments, it means "equals".
if [$vara = = $varb]
... Next slightly
!= is not equal to
Often seen in conditional judgments, it means "not equal to".
If [$vara!= $varb]
... Next slightly

^
This symbol represents the "start" position of a row in a regular expression.

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.