Some special symbols in the shell script _linux shell

Source: Internet
Author: User
Tags arithmetic comments mkdir

The special symbols commonly used in the shell are listed below:

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

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

|| && {Xx,yy,zz,...} ~ ~+ ~-& \\<...\\> +-%= = =!=


# Well No. (comments)
This is almost a full field of symbols, in addition to the previous mentioned \ "First line \"
#!/bin/bash
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
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 a 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 for the "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 quotes (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 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 operations as the "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)
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)) \"


\ Down 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 previous example, in the Echo, the \ $bkdir, Escape will cancel the function of the $ variable, so the $bkdir will be output, and the second $bkdir will output the contents of the variable/home.


| pipe (Pipeline)
pipeline is a UNIX system, a fundamental 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 logic, for example, in the condition detection, using!= to represent \ "Not equal to \"
if [\ "$?\"!= 0]thenecho \ "Executes error\" Exit 1fi
She plays the role of "anti-logical \" in a regular expression
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: \ "Nothing is done," but returns a status value of 0.
:
echo $? # response to 0
: > f.$$
The above line, the equivalent of Cat/dev/null >f.$$. 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.


? 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
At operation time, it represents \ multiplication \.
Let \ "Fmult=2*3\"
In addition to the built-in instruction let, there is also a instruction on the operation of expr, where the asterisk also serves as the "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 \ "secondary" 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=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).


Regular expressions for ${} variables
Bash defines a number of uses for ${}. The following is a table column taken from the line description
${parameter:-word} ${parameter:=word} ${parameter:?word} ${parameter:+word} ${parameterffset: Length} ${!prefix*} ${#parameter} ${parameter#word} ${parameter# #word} ${parameter%word} ${parameter%%word} ${ Parameter/pattern/string} ${parameter//pattern/string}


$*
$* 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 \ "$#\"


$? Status value (state 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 =)) echo-e \ "Inital value, a = $a \\n\" ((a++)) echo \ "After a++, a = $a \"

{} curly braces (block of code)
Sometimes it appears in the script, with a paragraph or paragraphs in the curly braces that end with a "semicolon \" or a set of variables.
# 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 there is a difference that 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, 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 wasn't for support, we'd have to write a few lines and repeat it a few times!


[] Bracket
often appear in the process control, play the role of the judge-style. if [\ "$?\"!= 0]thenecho \ "Executes error\" Exit1fi
This symbol plays 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 a regular expression. 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 want to avoid this situation, we have to add the symbol of "boundary \"
grep ' \ ' Filea


+ plus sign (plus)
in an expression, she is used to represent \ "add \".
Expr 1 + 2 + 3
In addition, in regular expressions, it is used to denote the meaning of the front character of \ "many \".
# 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 represent \ subtraction \.
Expr 10-2
It is also an option symbol for system directives.
ls-expr 10-2
In the GNU directive, it means "standard input \" If the symbol is used alone, without any additional file name. 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 \ 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 see in the conditional judgment, representing the meaning of \ "equals \".
if [$vara = = $varb]
... Next slightly

!= is not equal to
often seen in the conditional judgment, represents the meaning of \ "not equal to \".
If [$vara!= $varb]
... Next slightly


^
in a regular expression, this symbol represents the "start \" position of the row and, in [], the same as the "!\" (exclamation mark).


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 (the same as ' touch ').
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.

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.