Special symbols in the shell

Source: Internet
Author: User
Tags arithmetic

The special symbols commonly used in the shell are listed as follows: #;       ;;       .        ,/\ ' String ' |    !       $    ${}    $?       $$ $* "String" * * *?    : ^ $# [email protected] ' command ' {} [] [[]] () (()) | | && {Xx,yy,zz,...} ~ ~+ ~-& \<...\> +-%= = = = Output/input re-orientation > &GT;&G    T < <<: > &> 2&> 2<>>& >&2 # well number (CO Mments) This is almost a full-field symbol, in addition to the previous mention of the "first line" #!/bin/bash well is often at the beginning of a line, or after the complete instruction, such a case that the symbol is followed by the annotation text, will not be executed. # This lines 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 # at 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 ~; You can also add the name of an account directly after the symbol: CD ~user or as part of the path: ~/bin;~+ current working directory, which represents the current working directory, she and the built-in instructions The role of PWD is the same. # 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 functions as a "continuous instruction" is the 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) in the shell, the user should be aware that a dot represents the current directory, two dot represents 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 regular expression, 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 does not work, that is, he is treated as a general symbol and prevents any variable substitution. 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 front of the single double quotes, surrounded by a string, but if the string is a list of command columns, what will happen? 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 comma (comma) symbol is often used in operations 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 the directory when the path 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))" \ Inverted slash () in the interactive mode of escape character, there are several functions, placed before the command, there is a cancellation of the role of aliases, before the special symbol, the role of the special symbol disappears; At the end of the instruction, the table command to connect to 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, the output $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 Use this concept to streamline script. There is considerable help.! An exclamation point (negate or reverse) usually represents a function of anti-logic, such as conditional detection, which is represented by "not equal to" if ["$?"]! = 0]thenecho "executes error" exit 1fi in the regular expression she acted as "anti-logic" Role LS a[!0-9] above example, the representative shows other files except A0, A1 ... A9 these files. : Colon in bash, this is a built-in directive: "Nothing is done", but returns the status value 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. Sometimes, the following usage is also present: ${hostname?} ${user?} ${mail?} The purpose of this line is to check if these environment variables are set and the error messages will be displayed with standard errors if they are not set. Like this check if you use a similar TEST or if this kind of practice, basically can also deal with, but are not compared with the simplicity and efficiency of the above example. In addition to the above, there is a place where you must use the colon path= $PATH: $HOME/fbin: $HOME/fperl:/usr/local/mozilla in the user's own HOME directory. Bash_profile or any file with similar features , we use colons to do the partition when we set the "path". 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 as a "multiplication" role. But be careful in use, he must precede the escape character. * * The two asterisks in the operation represent the meaning of the "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} ${parameter:offset } ${parameter:offset:length} ${!prefix*} ${#parameter} ${parameter#word} ${parameter# #word} ${parameter% Word} ${Parameter%%word} ${parameter/pattern/string} ${parameter//pattern/string}$* references script. The execution reference variable, the algorithm referencing 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] have the same function as the $*, 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. $ #这也是与引用变量相关的符号, her role is to tell you how much 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 ID of the process is unique, it is not possible to have a repetitive PID at the same time. Sometimes, script. A temporary file is required to hold the necessary information. and this script. They 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 temporary file is written using a dynamic file name, if the SCRIpt. Other problems will arise if the implementation is not cleared after completion. 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 instruction group has an attribute, and the shell executes the set of instructions in order to generate Subshell. 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, the parentheses are also used in the definition of the array variable; Apply to other occasions where you might need to add escape characters, such as an expression. (()) 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 script. It will appear in curly braces with a section or paragraphs of instructions or variable settings that end with a "semicolon".  # 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 shell execution, does not produce subshell. Curly braces are also used in the function of functions. Broadly speaking, simply using curly braces acts like a function that does not have a specified name. So write the script like this. It's also a pretty good thing. This approach, especially for the redirection of output inputs, can be streamlined script. of complexity. In addition, curly braces have another use, as follows {Xx,yy,zz,...} This combination of curly braces, commonly used in the combination of strings, 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" exit 1fi this symbol acts as a "scope" or "set" role in a regular expression rm-r 200[1234] The example above, representing the deletion of 2001, 2002, 2003, 2 004 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 as the meaning of "boundary" in a regular expression. 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" symbol grep ' \ ' filea+ plus (plus) in the expression, which she uses to denote "addition". Expr 1 + 2 + 3 also in the regular expression, the meaning of the front character that is used to denote "many". # 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, do not add anyThe name of the file, representing the meaning of "standard input". 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 uses as arithmetic or judgment. = = equals is often seen in conditional judgments, which means "equals". if [$vara = = $varb] ... The next! = is not equal to what is often seen in conditional judgments, meaning "not equal". if [$vara! = $varb] ... Next ^ This symbol is in the regular expression, which represents the "start" position of the line.

  

Special symbols in the shell

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.