The special symbols commonly used in the shell are listed as follows: #;;; . ,/\ \ ' String ' |! $ ${} $? $$ $*\ "string\" * * *? : ^ $# [email protected] ' command ' {} [] [[]] () (()) | | && {Xx,yy,zz,...} ~ ~+ ~-& \\<...\\> +-%= = =!=# well (comments) This is almost a full-field symbol, in addition to the previous mention of the \ "first line \" #!/bin/bash The well number is often at the beginning of a line, or in the complete instruction , such cases indicate that the annotation text is followed by the symbol and will not be 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 # to the beginning of the line. This is often used in the writing process. #echo \ "A = $a \" # a = 0 if it is used in a directive, or if the quotation marks are enclosed in double quotes, or after the inverted slash, then he becomes a general symbol, without the special function described above. The home directory of the account is a common symbol, representing the user's home directory: cd ~, or you can add the name of an account directly after the symbol: CD ~user or as part of the path: ~/bin ~+ Current working directory, this symbol represents the current working directory, she and the built-in instruction PW The function of D is the same. # echo ~+/var/log ~-Last working directory, this symbol represents the last working directory. # echo ~-/etc/httpd/logs; The semicolon (command separator) in the shell, the symbol for the function "continuous instruction \" is \ "semicolon \". such as the following example: CD ~/backup; mkdir startup; CP ~/.* startup/.; The continuous semicolon (Terminator) is dedicated in the case option, serving as the Terminator role. Case \ "$FOP \" inhelp) echo \ "Usage:command-help-version filename\";; Version) echo \ "version 0.1\";; Esac. Comma (dot, which is "dot") in the shell, the user should be aware that a dot representsThe current directory, with two dots representing the upper directory. Cdpath=.:~:/home:/home/web:/var:/usr/local in the upstream Cdpath setting, the dot after the equal sign represents the meaning of the current directory. If the file name starts with Dot, the file is a special file, and the LS command must be added with the-a option to display. In addition, in RegularExpression,
A dot represents a match to a character. The ' string ' single quote quotation marks the enclosed content, which is treated as a single string. The $ sign that represents the variable within the quotation marks, has no effect,
That is, he is treated as a general symbol to prevent any substitution of variables. Heyyou=homeecho ' $heyyou ' # We get $heyyou \ "string\" double quotation marks (double quote) are enclosed in quotation marks and will be treated as a single string. It prevents wildcard expansion, but allows variable expansion. This is different from the way a single argument is handled. Heyyou=homeecho \ "$heyyou \" # We get home ' command ' inverted quotation marks (backticks) in the preceding single double quotation mark, enclosing a string, but what if the string is a list of command columns? The answer is no execution.
To deal with this situation, we have to use a single quotation mark. fdv= ' date +%f ' echo \ "Today $FDV \" The date +%f in the inverted quotation marks is treated as an instruction, and the result of execution is brought into the FDV variable. The use of this thing, I Baidu a bit, and $ () is the same. When a command is executed, it will first ",
Or the statement in $ () is executed once as a command, and then added to the original command to re-execute, for example: Echo ' ls ' will first execute LS to get xx.sh, and then replace the original command: Echo Xx.sh The result of the final execution is xx.sh so, what we usually encounter is to output the execution of a bunch of commands to a variable,
It is understandable that you need to surround yourself with this command-substitution character. , commas (comma in comma, punctuation) This symbol is often used in operations as a "partition \" Purpose. 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) in the path representation , the directory is represented. CD/ETC/RC.DCD. /.. CD/usually single/represents the root root directory meaning; in arithmetic, a symbol representing division. Let \ "NUM1 = ((a = 10/2, B = 25/5)" \ \ "The escape character in interactive mode has several functions; before the instruction, there is the function of canceling the aliases; Before the special symbol,
The function of the special symbol disappears; At the very end of the instruction, indicates that the command joins the next line. # type RMRM is aliased to ' rm-i ' # \\rm./*.log above, I add escape characters to the RM Directive, which is the function of temporarily canceling the alias and restoring the RM instruction. # bkdir=/home# echo \ "Backup dir, \ \ $bkdir = $bkdir \" Backup dir, $bkdir =/home above the \ \ $bkdir in echo, Escape will be the function of the $ variable is canceled, therefore, Outputs a $bkdir, and the second $bkdir outputs the contents of the variable/home. | Pipeline (pipeline) pipeline is a basic and important concept for UNIX systems. Connect the standard output of the previous command as the standard input for the next instruction. W.H.O. | Wc-l's use of this concept is very helpful for streamlining script. An exclamation point (negate or reverse) usually represents a function of the anti-logic, such as conditional detection, which represents \ "not equals \" If [\ "$?\"! = 0]thenecho \ "Executes error\" Exit 1fi in the regular expression. "Inverse logic \" role LS a[!0-9] above example, the representative shows except A0, A1 .... A9 Other files of these files. : Colon in bash, this is a built-in directive: \ "Nothing is done", but returns a status value of 0. : Echo $? # response is 0: > f.$$ above this line, equivalent to Cat/dev/null >f.$$. Not only is the writing brief, but also the implementation of a lot of efficiency. In some cases, the following usage is also present: ${hostname?} ${user?} The purpose of this line is to check if these environment variables are set and the error message will be displayed with standard errors if they are not set.
Such checks, like test or if, can basically be dealt with, but not as simple and efficient as the example above. The question mark (wild card) plays a role on the filename extension (filename expansion) to match an arbitrary character, but does not contain a null character. # ls A?A1 use her features to make more accurate file name matching. * Asterisks (wild card) are quite common symbols. On the file name extension (filename expansion), she is used to represent any character, including the null character. # ls a*a A1 access_log in 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, the asterisk here also acts as a "multiplication \" role.
But be careful in use, he must precede the escape character. * * The two asterisks at the time of operation represent the meaning of \ "second party \". Let \ "sus=2**3\" echo \ "sus = $sus \" # sus = 8$ Money number (dollar sign) variable substitution (Variable Substitution) represents the symbol. Vrs=123echo \ "VRS = $vrs \" # VRS = 123 In addition, the Regular Expressions is defined as the end of \ "line \" (End-of-line).
This is commonly used in grep, SED, awk, and Vim (vi). The regular expression of the ${} variable, bash, defines a number of uses for ${}. The following is a table column taken from the online description ${parameter:-word} ${parameter:=word} ${parameter:?word}
${parameter:+word} ${parameterffset} ${parameterffset:length} ${!prefix*} ${#parameter}
${parameter#word} ${parameter# #word} ${parameter%word} ${parameter%%word}
${parameter/pattern/string} ${parameter//pattern/string}$* $* references the execution reference variable of the script, the algorithm that references the parameter is the same as the general instruction, the instruction itself is 0, followed by 1, and so on. The reference variables are represented in the following ways: $ $, $ $, $ $, $ $4, $ $6, $7, $8, $9, ${10}, ${11} ..... Single digit, you can use the number directly, but more than two digits, you must use the {} symbol to enclose. $* is the symbol that represents all reference variables. When used, you have to add double quotes as appropriate. echo \ "$*\" also has a symbol that has the same effect as $*, but with a slightly different effect from the way it is handled. [Email protected] [Email protected] has the same function as $*, but they have a different point. The symbol $* treats all reference variables as a whole. However, the symbol [email protected] still retains the concept of the section of each reference variable. $# This is also the symbol associated with the reference variable, and her role is to tell you what the total number of reference variables is. echo \ "$#\" $? Status variable in general, the process of a UNIX (Linux) system ends with the execution of the system call exit (). This callback value is the status value.
Passed back to the parent process to check the execution status of the child process. If the execution succeeds, the callback value of the general instruction is 0; the failure is 1. Tar cvfz dfbackup.tar.gz/home/user >/dev/nullecho\ "$?\" $$ because the process ID is unique, it is not possible to have a repetitive PID at the same time. Sometimes, script will need to generate temporary files to hold the necessary information.
This script may also be used by users at the same time. In this case, the fixed file name is not reliable in the notation.
Only a dynamic file name can be generated to meet the needs. Symbolic $$ may be able to meet this requirement. It represents the PID of the current shell. echo \ "$HOSTNAME, $USER, $MAIL \" > ftmp.$$ use it as part of the file name to avoid overwriting the same file name at the same time. PS: Basically, the system will recycle the completed PID and then allocate it again as needed. So script even if the temp file is written using a dynamic file name,
If the script is not cleared after execution, there are other problems. The instruction Group (Command group) encloses a sequence of sequential instructions in parentheses, which is called a command group for the shell. As the following example: (CD ~; vcgh= ' pwd '; echo $vcgh),
The command group has a feature that the shell executes in order to generate Subshell to execute this set of instructions. Therefore, the variables defined therein are used only for the instruction group itself. Let's take a look at the example of cat Ftmp-01#!/bin/basha=fsh (A=INCG; echo-e \ "\\n $a \\n\") echo $a #./ftmp-01incgfsh In addition to the above instruction group, parentheses are also used in the definition of the array variable Other occasions, such as expressions, that may need to be used in addition to escape characters. (()) This set of symbols is similar to the Let directive, and is used in arithmetic operations, which is the built-in function of bash. Therefore, it is much better to perform efficiently than to use the Let command. #!/bin/bash ((a = ten)) echo-e \ "Inital value, a = $a \\n\" ((a++)) echo \ "After a++, a = $a \" {} curly braces (Block of code) sometimes SC Ript will appear in the curly braces with a paragraph or a few paragraphs with a "semicolon \" To do the end of the instruction or variable settings. # 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 with a different point, it's in the current s Hell execution, does not produce subshell. Curly braces are also used in the function \ function. Broadly speaking, simply using curly braces acts like a function that does not have a specified name.
So writing script like this is a pretty good thing. This approach, especially for output input redirection, streamlines the complexity of the script. In addition, curly braces have another use, the following {Xx,yy,zz,...} such a combination of curly braces, commonly used in string combinations, to see 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 few directories.
This set of symbols is quite extensive in applicability. If we can make good use of it, the payoff is streamlining and efficiency. Like the following example Chown Root/usr/{ucb/{ex,edit},lib/{ex?.? *,HOW_EX}} If it is not for the support of this usage, we have to write a few lines to repeat several times! [] The brackets often appear in the process control, playing the role of enclosing judgment. if [\ "$?\"! = 0]thenecho \ "Executes error\" exit1fi this symbol acts like \ "Scope \" or \ "set \" in the regular expression Rm-r 200[1234] above example, on behalf of Delete 2001, 200 2, 2003, 2004, etc. the meaning of the directory. [[]] This set of symbols is essentially the same as the previous [] symbol, but she allows it to be used directly in it | | and && logic and other symbols. #!/bin/bashread Akif [[$ak > 5 | | $ak < 9]]thenecho $AKFI | | Logical symbols This is often seen, representing the or logical symbols. && logical Symbols This is also often seen, representing and logical symbols. & Background Work order A & symbol, placed at the end of the complete instruction column, means that the instruction column is put into the background to work. Tar cvfz data.tar.gz data >/dev/null&\\<...\\> word boundaries This set of symbols is defined in the rule expression as the meaning of \ "boundary \". For example, when we want to look for the word, if we use grep the FileA you will find that words such as there are also considered to be a matching word. Because the coincidence is part of the there.
If we want to avoid this situation, we have to add the "boundary \" of the symbol grep ' \ \ ' filea+ Plus (plus) in the expression, she used to represent \ "addition \". Expr 1 + 2 + 3 In addition, in the rule expression, the meaning of the preceding character of \ "many \" is used. # grep ' 10\\+9 ' fileb109100910000910000931010009# This symbol must be preceded by escape characters when used. -Minus sign (dash) in the expression, she is used to denote \ "subtraction \". Expr 10-2 is also an option symbol for system directives. Ls-expr 10-2 in the GNU directive, if you use the-symbol alone, without any added file name, the meaning of \ "standard input \" is represented. This is a common option for the GNU Directive. For example, the following example, Tar XPVF-here is a symbol that represents both reading data from a standard input. However, the CD instruction compares the special CD-this represents the change of working directory to the \ "last \" working directory. % Division (Modulo) is used in expressions to denote \ "division \". Expr 10% 2 In addition, the following ${parameter%word}${parameter%%word} is also used in the regular expressions about variables: one% represents the shortest word match, and two represent the longest word match. = equals is a symbol that is often seen when a variable is set. Vara=123echo \ "Vara = $vara \" or the setting of PATH is even applied to such purposes as arithmetic or judgment. = = Equals (equals) is often seen in conditional judgments, representing the meaning of \ "equals \". if [$vara = = $varb] ... The next! = is not equal to what is often seen in conditional judgments, representing \ "Not equal to \" meaning. if [$vara! = $varb] ... Next ^ This symbol in the regular expression, the \ "start \" position of the row, and in [] also with \ "!\" (exclamation point) as "non" output/input redirection > >> < <<:> &> 2&> 2< The;>>& >&2 file descriptor (filename descriptor), which represents a file with a number (typically 0-9). Commonly used file descriptors are as follows: File descriptor name common abbreviation default value 0 standard outputInto the STDIN keyboard 1 standard output STDOUT screen 2 standard error Output stderr screen we use a simple < or >, equivalent to Using 0< or 1> (described in detail below).
* cmd > file redirects the output of the cmd command to the filename. If file already exists, empty the original, and use Bash's noclobber option to prevent the old file from being covered.
* cmd >> file redirects the output of the cmd command to the filename and, if file already exists, adds the information to the back of the original file.
* cmd < file to make cmd command read from file
* cmd << text reads input from the command line until a line with the same text ends. Unless you enclose the input in quotation marks, this mode will replace the shell variable with the input.
If you use <<-, you will ignore the tab at the beginning of the line, the end row can also be a bunch of tabs plus a text with the same content, you can refer to the following example.
* cmd <<< word put word (instead of file word) and the back line as input to CMD.
* cmd <> file redirect files to input in read/write mode, file files will not be corrupted. It makes sense only when the application takes advantage of this feature.
* cmd >| The file function is the same, but even when the noclobber is set, the file is covered with a | rather than a few books! Currently, only >! is used in CSH to achieve this function. : > FileName truncates the file \ "filename\" to 0 length. # If the file does not exist, create a 0-length file (the same effect as ' touch '). CMD >&n send output to file descriptor n cmd m>&n redirect output to file descriptor n cmd >&-turn off standard output cmd <&n input from file descriptor n cmd m< &n m from file description each n cmd <&-turn off standard input cmd <&n-move the input file descriptor n rather than copy it. CMD >&n-moves the output file descriptor n rather than copying it. Note: >& actually duplicates the file descriptor, which makes the cmd > file 2>&1 different from the cmd 2>&1 >file.
Special symbols in shell scripts