Special symbols commonly used in the shell

Source: Internet
Author: User
Tags arithmetic mkdir

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

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

\ string\ * **  ?: ^ $#   $@ ' command ' {}   [] [[]] () &nbs p;  (())  

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

   
# Well (comments)  
This is almost a full field symbol, except for the "first line \"   that was previously mentioned;
#!/ BIN/BASH&NBSP
The well numbers often appear at the beginning of a line, or after a complete instruction, which indicates that the callout text is followed by the symbol and is not executed. &NBSP
# This lines is comments. 
echo \ "A = $a \" # a = 0 
Because of this attribute, when you temporarily do not want to execute a line of instructions, just add a # at the beginning of the line. This is commonly used in the writing process. &NBSP
#echo \ "A = $a \" # a = 0 
if used in a directive, or in quotation marks or double quotes, or after a slash, he becomes a general symbol without the special features described above. &NBSP


~ 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


. Dot (dot, is "dot")
(1) In the shell, users should be aware that a dot represents the current directory, and 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.
(2) If the file name begins with Dot, the file is a special file, and the LS instruction must be added with the-a option to display.

(3) 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 single quotation mark 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 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 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 interactive mode, the escape character has several effects:

Before the instruction, there is the function of canceling aliases;

Before the special symbol, the function of the special symbol disappears;

At the very end of the instruction, which indicates that the instruction connects to 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} ${parameterffset:l Ength} ${!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 \ "$#\"


$? 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 =)) 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 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 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

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.