Detailed description of special symbols used in Linux
Original: https://www.cnblogs.com/lidabo/p/4323979.html
# pound (comments)
#管理员 $ normal User
In the script
#!/bin/bash #!/bin/sh
The pound sign also often appears at the beginning of a line, or after the complete instruction, which indicates that the symbol is followed by the annotation text 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 instruction, simply add # at the beginning of the line. This is often used in the writing process.
#echo "a = $a" # a = 0
If used in a directive, or surrounded by double quotation marks, or behind a slash, then he becomes a general symbol without the special function described above.
=======================================================================================
~ Account's Home directory
On behalf of 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
~+ the current working directory, this symbol represents the current working directory, and the role of her and the built-in command 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/.
=====================================================================================
;; Continuous semicolon (Terminator)
Dedicated in case options, assume the role of Terminator.
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 represents the current directory, and two dots represent the upper-level directory.
Cdpath=.:~:/home:/home/web:/var:/usr/local
In the upstream Cdpath setting, the dot after the equals 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.
===================================================================================
' String ' single quotation mark (quote)
The enclosed quotation marks are 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 mark (doubles quote)
The content enclosed in double quotes is 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, enclose the 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 taken into the FDV variable.
====================================================================================
, comma (commas in comma, punctuation)
This 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
The escape character in interactive mode has several functions; before the instruction, there is the function of canceling the aliases, and before the special symbol, the function of the special symbol disappears; placed at the very end of the instruction, indicating that the command joins the next line.
# type RMRM is aliased to ' rm-i ' # \rm./*.log
In the example above, I add the escape character 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
In the above example, the \ $bkdir in echo, Escape will be the function of the $ variable is canceled, therefore, the output $bkdir, and the second $bkdir will output the contents of the variable/home.
=======================================================================================
| Pipe (Pipeline)
is a UNIX system, a fundamental and important concept. Connect the standard output of the previous command as the standard input for the next instruction.
W.H.O. | Wc-l
The use of this concept is very helpful in streamlining script.
====================================================================================
! Exclamation point (negate or reverse)
Usually it represents the function of anti-logic, such as conditional detection, in! = to represent "not equal"
If ["$?"! = 0]thenecho "executes error" Exit 1fi
She acts as a "counter-logical" role in regular expressions.
LS A[!0-9]
In the example above, the representative shows other files except A0, A1 ... A9 these several files.
=====================================================================================
: Colon
In bash, this is a built-in directive: "Nothing is done", but returns the status value of 0.
:
echo $? # response is 0
: > F.[Math processing Error]. Not only is the writing brief, but also the implementation of a lot of efficiency.
Sometimes, the following uses of this kind are 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. Such checks, like test or if, can basically be dealt with, but not as simple and efficient as the example above.
In addition to the above, there is a place where you must use a colon
In the user's own home directory, in the. bash_profile or any feature-like file, set the "path" for the occasion, we use colons to do the partition.
====================================================================================
? Question mark (wild card)
The role played on the filename extension (filename expansion) is to match an arbitrary character, but does not contain a null character.
# ls A?A1
Make use of her characteristics, you can do more accurate file name matching.
* asterisk (wild card)
A fairly common symbol. On the file name extension (filename expansion), she is used to represent any character, including the null character.
# ls a*a A1 Access_log
It represents "multiplication" when it is being operated on.
Let "fmult=2*3"
In addition to the built-in instruction let, there is also an instruction expr about the operation, where the asterisk also acts as the "multiplication" role. But be careful in use, he must precede the escape character.
======================================================================================
* * Sub-square operation
Two asterisks represent the meaning of the "sub-square" during operation.
Let "Sus=2**3" echo "sus = $sus" # sus = 8
=====================================================================================
$ money (dollar sign)
The delegate symbol for the variable substitution (Variable Substitution).
vrs=123 echo "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).
=====================================================================================
Regular expressions for ${} variables
Bash defines a number of uses for ${}. Here are the table columns taken from the online description
$*
$* references a script's execution reference variable, 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 variable is represented in the following way:
$ $, $ $, $ $, $ $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 "$*"
There is also a symbol that has the same effect as the $*, 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 "$#"
$? State value (status variable)
Generally, 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 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 temporary file is written using a dynamic file name, there are other problems if the script is not cleared after execution.
====================================================================================
() instruction Group (command Group)
Enclose a sequence of successive 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 look at an example.
# cat Ftmp-01#!/bin/basha=fsh (A=INCG; echo-e "\ n $a \ n") echo $a #./ftmp-01incgfsh
In addition to the above-mentioned instruction group, parentheses are used in the definition of the array variable, as well as in other occasions where the escape character may need to be used, 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 the script appears, and in curly braces there is a paragraph 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 executes in the current shell and 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 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, as follows
{Xx,yy,zz,...}
This combination of curly braces, commonly used in the combination of strings, 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 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 were not for the support of this usage, we have to write a few lines to repeat several times!
[] Brackets
Often appear in the process control, play the role of the surrounding judgment. If ["$?"! = 0]thenecho "executes error" Exit1fi
This symbol acts like a "scope" or "set" in a regular expression
Rm-r 200[1234]
The above example, represents the deletion of 2001, 2002, 2003, 2004 and so on the meaning of the directory.
[[ ]]
This set of symbols is basically 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 will often be seen, representing the or logical symbols.
(go) detailed description of special symbols used in Linux