Special symbols summarizes [Posts] in the shell

http://www.embeddedlinux.org.cn/emb-linux/entry-level/201907/18-8747.html

 

In the shell used special symbols are listed below: 

#; ;;, / \\ 'String' | $ $ $ $$ $ {} *.!? 

\ "String \" * **: ^ $ # $ @ `? {} command` [] [[]] () (()) 

|| && {XX, YY, ZZ, ...} ~ + ~ ~ - & \\ <... \\> + - =% = =! = 

   
# pound sign (comments) 
which is almost a full field has a symbol, in addition to the previously already mentioned \ "first line \" 
#! / bin / bash 
pound sign often appears at the beginning of a line, or in after complete instructions, such cases indicates that the following symbol is a comment text, it will not be executed. 
This IS Comments Line #. 
Echo \ "A = $ A \" # A = 0 
Because of this feature, when the temporary do not want to execute a command line, just at the beginning of the line with the # on the line. This is commonly used in the writing process. 
#echo \ "a = $ a \  "
# A = 0 if it is used in the instruction, or quotation enclosed in double quotes, or if later the backslash, he became general symbols, does not have the above-mentioned special features. 


~ Home directory account 
be regarded as a common symbol, representing the user's home directory: cd ~; can also directly after the sign with the name of an account: cd ~ user or as a part of the path: ~ / bin 
~ + current working directory, this symbol represents the current working directory, and built-in command pwd her role is the same. 
Echo ~ + # / var / log 
~ - the last working directory, this symbol represents the last working directory. 
Echo ~ # - / etc / the httpd / logs 


; semicolon (Command separator) 
in the shell, as a \ "continuous instruction \" symbol is the function \ "semicolon \." For example the following examples: CD ~ / Backup; mkdir Startup; CP ~ /.* Startup /. 


;; continuous semicolon (Terminator) 
specific options in the case, as Terminator role. 
Case \ "$ FOP \" inhelp) echo \ "Usage: the Command -help -version filename \" ;; Version) echo \ "Version 0.1 \" ;; esac 


. comma (dot, is the "point") 
in the shell, users should all know that a dot represents the current directory, two dot represents the parent directory. 
CDPATH = .: ~: / home:  / home / web: / var: / usr / local
meaning CDPATH set up, the dot after the equal sign is representative of the current directory. 
If the file name begins with dot, which is a special file on the file, use the ls command must add -a option appears. In addition, in regularexpression, one dot on behalf of a matching character. 


'string' single quotation marks (single quote) 
Content enclosed with single quotes, and will be treated as a single string. The $ symbol represents the variables in quotation marks, no effect, that is to say, he was generally regarded as a symbol, and preventing any variable substitution. 
= homeecho HeyYou 'HeyYou $' # $ HeyYou We GET 


\ "String \" double quotation marks (double quote) 
is enclosed with double quotation marks content will be treated as a single string. It prevents wildcard expansion but allow variable expansion. This treatment with different numbers of single primer. 
= homeecho HeyYou \ "$ HeyYou \" # We GET Home 

`command` down quotes (backticks) 
in front of the single and double quotation marks to enclose a string, but if the string is a command line, what will happen? The answer is not performed. To deal with this situation, we have to use single quotes down to do. 
`DATE = +% fdv F`echo \" Today $ fdv \ " 
DATE within the inverted quotes +% F will be treated as an instruction, the result of execution will bring fdv variable. 


, Comma (comma, punctuation comma) 
this symbol is often used in the operation which as \ "segment \" purposes. In the following example 
! # / Bin / bashlet \ " t1 = ((a = 5 + 3, b = 7 - 1, c = 15/3)) \" echo \ "t1 = $ t1, a = $ a, b = b $ \ " 


/ slash (forward slash) 
in the path, said on behalf of the directory. 
cd /etc/rc.dcd ../ .. 

the let \ "num1 = ((A = 10/2, B = 25 /. 5)) \" 


\\ backslash 
escape characters in the interactive mode, there are several functions; placed before the instruction of cancellation of aliases effect; placed before a special symbol, the symbol of the special effect disappears; endmost over the instruction, the instruction indicates the next line is connected. 
# Type rmrm is aliased to `rm  -i '# \\ rm ./*.log
the above example, I added escape characters before the rm command, is to temporarily cancel the alias functionality, reducing the rm command. 
Bkdir = # / Home # echo \ "Backup dir, bkdir \\ $ = $ bkdir \" Backup dir, $ bkdir = / Home 
\\ $ bkdir in the cases of the echo, escape the $ variables function canceled, therefore, content will output $ bkdir, while the second will be $ bkdir output variables / home. 


| Pipeline (Pipeline) 
Pipeline is a UNIX  system, basic and important concepts. Standard coupling the output instruction, the next instruction as standard input. 
who | wc -l 
advantage of this concept, there is a considerable help to streamline the script. 


! Exclamation point (negate or reverse) 
action usually it represents the inverse logic, such conditions are detected, the use! = To represent \ "not equal to \" 
IF [\ "$? \"! = 0] thenecho \ "Executes error \ "exit 1fi 
in regular expressions she served \" 
ls a [! 0-9] 
on the embodiment, in addition to a display representative of a0, a1 .... a9 these other files files. 


: Colon 
in bash, which is a built-in instructions: \ "What else to do \", but returns a status value 0. 

Echo $ # responded to 0? 
:.> F $$ 
above this line, the equivalent of cat / dev / null> f $$ .. Not only the written brief, and execution efficiency is good on many. 
Sometimes, this type of usage of the following will occur 
: $ {HOSTNAME?} $ {  USER?} $ {MAIL?}
Role of this line is to check whether these environment variables have been set, there is no set standard error will display an error message. If this approach as check or if similar kind of test, the processing can be substantially, but not as both the simplicity and efficiency of the embodiment. 


? Question mark (wild card) 
on the file name extension (Filename expansion) the role is to match an arbitrary character, but does not contain null characters. 
# Ls a? A1 
take advantage of her features, it can do more precise file names match. 


* The asterisk (wild card) 
is quite commonly used symbols. On the file name extension (Filename expansion), she used to represent any character, contains null characters. 
# Ls a * a a1 access_log 
during operation, it represents \ "multiply \." 
let \ "fmult = 2 * 3  \"
In addition to the built-in commands let, as well as instructions on a calculation of expr, asterisk here as \ "multiply \" role. But be careful in the use, he must be preceded by escape characters. 


** power calculation 
two asterisks on behalf of \ "power \" means at the time of operation. 
the let \ "SUS = 2 ** 3 \" echo \ "$ SUS SUS = \" # SUS = 8 


$ No money (dollar sign)  
variable substitution (Variable Substitution) representative symbol. 
vrs = 123echo \ "vrs = $  vrs \" # vrs = 123
In addition, is defined as a \ "line \" extreme end (end-of-line) in the Regular Expressions. This is often used in grep, sed, awk and vim (vi) of them. 


Regular expressions $ {} variables 
bash for $ {} defines a lot of usage. The following description is taken from the line tabulated 
   $ {parameter: -word} $ { parameter: = word} $ {parameter:? Word} $ {parameter: + word} $ {parameterffset} $ {parameterffset: length} $ {



* $  Cited reference variable script execution, the instruction references the same general algorithm parameters, instruction itself is 0, followed by 1, and so on. Reference variable representative follows: 
$ 0, $ 1, $ 2, $ 3, $ 4, $ 5, $ 6, $ 7, $ 8, $ 9, $ {10}, $ {11} ..... 
single digits, can be used as a digital , but two or more digits, you must use the {} symbols enclosed. 
$ * It is on behalf of all symbolic references variables. When used, subject to availability double quotes. 
echo \ "$ * \" 
symbol and there is a $ * has the same effect, but the effectiveness of the approach is slightly different symbols. 


$ @ 
$ @ $ * And symbols have the same effect, but they both have a different point. 
Symbol $ * All references to the variable as a whole. $ @ Symbol but still retains the concept of each section of the reference variable. 

$ # 
This is related to the reference variable symbol, her role is to tell you that the total number of reference variables is. 
echo \ "$ # \" 


$? state values  (status variable) 
In general, the process of UNIX (linux) system to perform the system call exit () to end. The return value is the status value. Back to the parent process, used to check the execution state of the child process. 
General instruction program if executed successfully, its return value is 0; 1 failure. 
tar cvfz dfbackup.tar.gz / home / user>  / dev / nullecho \ "$? \" $$
Since the ID process is unique, at the same time, there can be repetitive PID. Sometimes, script will need to generate temporary files, used to store the necessary information. And this script may also be used at the same time users are. In this case, the file name is fixed on the wording on the obvious unreliable. Only generate dynamic file name, in order to meet the need. $$ symbols may be able to comply with this demand. It represents the PID of the current shell. 
echo \ "$ HOSTNAME, $ USER  , $ MAIL \"> ftmp. $$
to use it as a part of the file name can be avoided at the same time, resulting in the phenomenon of covering the same file name. 
ps: Basically, the system will recover PID is finished, and then allocated as needed to use again. Therefore, even if the script temporary files are written using dynamic file name, if the script is finished it is still to be cleared, it will create other problems. 

() Command group (command group) 
enclose the enclosed instruction string of continuous, this usage is for the shell, called the command group. As the following examples: (cd ~; vcgh = ` pwd`; echo $ vcgh), a command group characteristics, shell will generate subshell to execute the set of instructions. Thus, the variables defined therein, acts only on the group of instructions itself. We look at an example 
! # Cat ftmp-01 # /  bin / basha = fsh (a = incg; echo -e \ "\\ n $ a \\ n \"). Echo $ a # / ftmp-01incgfsh
addition to the group of instructions, also used in parentheses array variable defined; in addition also other applications may need an escape character to use in applications such as expression. 


(()) 
Action and let the set of instructions similar symbols, used in the arithmetic operation, is built bash function. So, in the implementation efficiency is better than let a lot of instruction. 
#! / bin / the bash ((A = 10)) echo -e \ "inital value, A = $ A \\ n-\" ((A ++)) echo \ "After A ++, A = $ A \" 

{} Great brackets (Block of code) 
sometimes the script which will appear in curly brackets will be sandwiched to a few paragraphs in a \ "semicolon \" do at the end of the command or set variables. 
# Cat ftmp-02 # / bin  / basha = fsh {a = inbc; echo -e \ "\\ n $ a \\ n \"}!. Echo $ a # / ftmp-02inbcinbc
such usage instructions described above Groups are very similar, but there are different points, which in the current shell execution, no subshell. 
Braces are also used in the \ "function \" function. Broadly speaking, when only a simple to use braces, acts like a function does not specify the name of the general. Therefore, write script is quite a good thing. Especially for redirecting the output of the input, this approach can streamline complexity of the script. 

Further, there is another use of braces as follows 
{xx, yy, zz, ...  }
such a combination of braces, often used in combination string, look at an 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 directory . This set of symbols on a fairly wide applicability. To be able to make good use of it, the rewards are streamlining and efficiency. Like the example below 
chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}} 
if not for the support of this usage, we have to write a few lines repeated several times ah! 


[] Brackets 
often appear in the process control, the role played enclose determination formula. if [\ "$? \"  ! = 0] thenecho \ "Executes error \" exit1fi
this as a sign similar to a regular expression \ "range \" or \ "collection \" role 
rm -r 200 [1234] 
on cases, on behalf of deleting means 2001, 2002, 2003, 2004 etc directory. [[]] That the previous set of symbols [] symbols, substantially the same effect, but she was able to directly access && and || logical symbols such therein. #! / bin / bashread Akif [[AK $> $. 5 || AK <. 9]] thenecho $ akfi  || logic symbol this will always see symbol represents or logic. && logical symbol that will often see that symbol represents and logic. Background Work & single ampersand, and in the rearmost end of the complete instruction sequence, means that the instruction sequence into the background work. Data cvfz the data.tar.gz the tar> / dev / null &  \\ <... \\> a word boundary which set of symbols in the regular expression, is defined as \ "border \" the meaning of. For example, when we want to find the word that, if we use grep the FileA 


 




 



 



 



 


You will find, like the kind of word there, it will be treated as matching word. Because the there happened to be a part of. If we are to be avoided in this case, you have to add \ "border \" symbol 
grep '\\' FileA 


+ Plus (PLUS) 
in the calculation formula, she is used to represent \ "addition \." 
expr 1 + 2 + 3 
Further, in the regular expression used to represent \ "a number of \" previous character meaning. 
# Grep '10 \\ + 9 'fileB109100910000910000931010009 # symbol, when used, must be preceded escape character. 


- minus sign (dash) 
in the expression, she used to indicate \ "subtraction \." 
expr 10 - 2 
Further options are symbology instructions. 
ls -expr 10 - 2 
in the GNU instruction, if used alone - when symbol, without adding any of the file name, the representative \ "standard input \" means. This is a common option GNU instruction. For example the embodiment 
tar xpvf - 
here - the symbol represents both read data from the standard input. 
However, it is rather special cd command 
cd - 
this represents change the working directory to the \ "last \" working directory. 


% Division (Modulo) 
in the calculation formula used to represent the \ "dividing \" .
expr 10% 2 
In addition, also be used in regular expressions regarding variables among the following 
$ {parameter% word} $ {  parameter %% word}
one percent represents the shortest word matching, two word represents the longest match. 


Equals sign = (Equals) 
often seen when setting the variable symbols. 
vara = 123echo \ "vara = $  vara \"
or the like PATH is set, or even applied to the arithmetic formula, etc. Analyzing such uses. 


== equal sign (Equals) 
often seen in the conditional formula, the representative \ "equal \" means. 
IF [Vara == $ $ varB] 
... & c 

! = not equal 
often seen in conditional formula, the representative \ "is not equal to \" means. 
IF [$ Vara! = $ varB] 
... & c 


^ 
symbol in the regular expression, \ "beginning \" represents the position of the line, said the \ "! \" (exclamation point) as in [] in " non " 


output / input redirection 
> >> <<<:> &> & 2> 2 <>> &> &  

2 file descriptor (File Descriptor), with a number (typically 0-9) to indicate a file. 
Common file descriptor as follows: 
descriptor Abbreviation Name Default value 
0 the stdin keyboard 
a screen to stdout 
2 stderr standard error output screen 
when we simply use <or>, 0 is equivalent to using <1 or> (described in detail below). 
* Cmd> file 
to redirect output to a file cmd command in the file. If the file already exists, empty the original file, use the bash noclobber option to prevent coverage of the original file. 
* Cmd >> file 
to redirect output to a file cmd command file, if the file already exists, put the information behind the increase in the original file. 
* Cmd <file 
so that read from the command cmd File 
* cmd << text 
to read input from the command line, the same until the end of a text line. Unless the input enclosed in quotation marks, will enter this mode, the contents of which shell variable substitution. If you use << - will ignore the next input line of the first tab, the end of the line may be a bunch of tab content on the same text together, can refer to the examples that follow. 
* Cmd <<< word 
the word (rather than a file word) and the following is provided to wrap cmd as input. 
* Cmd <> file 
in read-write mode to redirect the file input file, the document file is not destroyed. Only when the application takes advantage of this feature, it is meaningful. 
* Cmd> | file 
functions with>, but also covering Even when set noclobber file documents, pay attention to is |!! Instead of some of the book said, currently only still in use in csh> achieve this function . 
:> Filename    The document \ "filename \" (same as 'touch' effect) truncated to 0 length. # If the file does not exist, then creating a file of zero length. 
Cmd> & n-  output file descriptors to n- 
cmd m> & n  outputs the m symbols to the information file to redirect file descriptor n- 
cmd> & -  Close standard output 
cmd <& n  input from the file descriptor n- 
cmd m <& n  m from the description of each file n- 
cmd <& -  Close standard input 
cmd <& n-  movement input file descriptor n instead of copying it. (Explanation required) 
cmd> N- &  moving the output file descriptor n not copy it. (Explanation required) 
Note:> & actually copy the file descriptor, which makes cmd> file 2> & 1 with cmd 2> & 1> file is not the same effect.

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11350182.html