A detailed explanation of the special symbol usage in Linux __linux

Source: Internet
Author: User
Tags arithmetic mkdir

# Well No. (comments)
#管理员 $ ordinary User

In the script

#!/bin/bash #!/bin/sh

Typically appear in the first line of the shell script, meaning to use/bin/bash to execute the current shell script, except for the first line, which plays the role of annotation. (in the shell script)

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

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, 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, is "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 RegularExpression, 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 happens 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, commas in punctuation)

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))"

====================================================================================

\ inverted Slash

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 RMRM 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)

is the UNIX system, the basic 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.[math processing Error]. 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

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=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 ${}. The following is a table column taken from the line description


$*
$* refers to the script's execution reference variable, and 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 a number directly, but more than two digits, you must use the {} symbol to enclose.
$* is the symbol representing all the reference variables. When used, it is necessary to enclose the case with double quotes.
echo "$*"
There is also a symbol with the same effect as the $*, but the utility is slightly different from the way it is handled.

$@
$@ and $* have the same symbolic effect, but they have a different point.
The symbolic $* treats all reference variables as a whole. But the symbol $@ still retains the section idea of each reference variable.

$#
This is also the symbol associated with the reference variable, and her role is to tell you the total number of reference variables.
echo "$#"

$? State value (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 general 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 impossible 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.$$
Use it as part of the filename 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 the use of dynamic filename, if the script is finished after the execution is still not clear, can cause other problems.

====================================================================================

() instruction Group (command Group)
Enclose a sequence of consecutive instructions in parentheses, which is called a command 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 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 instruction group mentioned above, parentheses are also used in the definition of array variables, and in addition to other situations where you may need to add an escape character, such as an expression.


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

{} curly braces (block of code)
Sometimes it appears in the script, and in curly braces there is a paragraph or a number of instructions or variables that end with a semicolon.
# cat FTMP-02#!/BIN/BASHA=FSH{A=INBC echo-e ' \ \ $a \ n '}echo $a #./ftmp-02inbcinbc
This usage is very similar to the instruction group described above, but there is a difference that it executes in the current shell and does not produce subshell.
Curly braces are also used in functions of the function. 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, 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 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 weren't for support, we'd have to write a few lines and repeat it several times.

[] Bracket
Often appear in the process control, play the role of the judge-style. If ["$?"!= 0]thenecho "executes error" Exit1fi
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

==============================================================================

|| Logical symbols
This will often be seen as symbols representing or logic.

&& logical Symbols
This will also be seen often, 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 when the symbol is used alone, without any file name 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, in a regular expression, represents the "start" position of the line, and in [] is also the "!" (exclamation mark) means "non"

============================================================================
Output/Input redirect
> >> < <<:> &> 2&> 2<>>& >&2

A file descriptor (descriptor) that represents a file with a number (usually 0-9).
The common file descriptors are as follows:
File descriptor names Common abbreviations default values
0 Standard Input stdin keyboard
1 Standard output stdout screen
2 standard error Output stderr screen
When we simply use < or >, it is equivalent to using 0< or 1> (described in detail below).
* cmd > file
Redirect the output of the cmd command to file files. If file already exists, empty the existing file and use Bash's noclobber option to prevent the original file from being covered.
* cmd >> file
Redirect the output of the cmd command to file files, and if file already exists, add the information to the back of the original file.
* CMD < file
To make the cmd command read from file
* cmd << text
Reads the input from the command line until a line that is the same as text ends. This mode replaces the input with shell variables unless you enclose the input in quotes. If you use <<-, you will ignore the tab at the beginning of the line, and the end row can be a bunch of tabs plus one with the text, and you can refer to the example behind it.
* CMD <<< Word
Supply the word (instead of the file word) and the wrapping line on the back side as input to CMD.
* cmd <> file
File files are redirected to input in read-write mode and file files are not corrupted. It makes sense only when an application exploits this feature.
* cmd >| File
Features, but even when the noclobber is set to cover file files, pay attention to the use of the | rather than some of the book said!, currently only in the CSH still follow the >! implementation of this function.
: > FileName truncated file "filename" to 0 length. # If the file does not exist, create a 0-length file (same as ' touch ' effect).
CMD >&n to send output to file descriptor N
CMD m>&n redirects the output to the file character m to the file descriptor n
CMD >&-off standard output
CMD <&n input from file descriptor n
CMD m<&n m from file description each n
CMD <&-close Standard input
CMD <&n-Move the input file descriptor n rather than copy it. (Need explanation)
CMD >&n-moves the output file descriptor n rather than copying it. (Need explanation)
Note: >& actually duplicates the file descriptor, which makes cmd > file 2>&1 and cmd 2>&1 >file the same effect.

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.