[Shell] shell basic usage

I hope not white piao, the point of a brush praise or comments to walk, as well as prohibit [Reserved]
* This article is to explain the variables in the shell of what (note I do more, made only slowly, If there is a particular need a certain aspect, you can reply in the comments, if I have the relevant information I will give priority to the issue)
this article will not teach you how to use cd, ls, pwd this basic fundamental to no longer the shell command. This article wrote the following aspects:

File name substitution
Command substitution
The arithmetic substitution
Processing> special filename - escaped characters \
apostrophe
Distinction> single quotes and double quotes - double quotes

File name substitution

Filename substitution (Globbing):? * [] → popular point that is commonly used fuzzy search fuzzy matching
these characters are used to match known as the wildcard (Wildcard), as follows:
Wildcards
* Matches zero or more of any character
? matches any character
[several characters] first appeared in square brackets match any one character
ls /dev/ttyS*# listed in the / dev / ttyS file names that begin with the
ls ch0?.doc# list ch0 [here can be anything] .doc
ls ch0[0-2].doc# list ch01. ch02.doc ch00.doc DOC
ls ch[012] [0-9].doc# listed ch00.doc ch01.doc ch02.doc ch03.doc like intermediate double-digit word to a 0,1,2 one from 0,1,2,3,4, 5,6,7,8,9

Note, Globbing the matching file names are expanded by Shell, that is to say before the parameters passed to the program have not been launched, such as the above ls ch0 [012] .doc command, if the current directory ch00.doc and ch02 .doc, then passed to the ls command parameter is actually two file name, not a matching string.

Command substitution

Command substitution: $()or reference to `` up with
Shell to execute the command, and then outputs the result immediately substitution to the current command line . First, look at us, `` such as the definition of a variable to store the output of the date command:
DATE = date
echo $ {DATE}

Command substitution can () expressed in $:
DATE = $ (DATE) # command of the line above exactly the same, this bar early or late to write a skillful use above that, as written above the relatively fast

Arithmetic substitution

Arithmetic substitution: $ (())
for arithmetic calculation, $ (()) in the Shell value to an integer variable, the same meaning $ [] is equivalent to, for example: VAR=45
echo $(($VAR+3)) #结果是48
$ (()) are used only ± * / and () operator, and can only do integer arithmetic!
$ (()) with only the ± * / and () operator, and can only do integer arithmetic!
$ (()) in can only ± * / and () operator, and can only do !!! integer arithmetic

according to binary digital calculates
$ [base # n], which represents a base band, construed in accordance with n-ary base, and then back there are several operations, explained in decimal.
echo $[2#10+11] #2的十进制就是2,11的十进制是11,他们两个相加就是13
echo $[8#10+11] #8的十进制是8,11的十进制是11,他们两个相加就是19
echo $[10#10+11] #10的十进制就是10,11的十进制是11,他们两个相加是21

Escape character \

C language and the like, \ Shell is used as the escape character, for the removal of a single character immediately following the special meaning (except for carriage return ), in other words, immediately following taking literal character. For example:
echo $SHELL
/ bin / bash
echo \$SHELL
$ SHELL
echo \\
\
For example, create a file named "$ $" can be so:
Touch \ $ \ \ $
this is to put $ escape, and then put a space to escape, and finally to give $ escaped. Work is not possible to write such things, is tantamount to commit suicide, so that we know like.

Although there is not a character has a special meaning, but it does use a file name too much, that is, - number. If you want to create a file name - the beginning of the file number, this will not work:
touch -hello
If you enter the above command will appear the following error:
Touch: invalid the Option - H
. Touch the Try --help 'for More Information
even with \ turn righteousness or incorrect report:
touch \-hello
Touch: invalid the Option - H
the Try Touch --help 'for More Information.

Regarded as a variety of UNIX command - command-line arguments that begin with numbers as command options, and not as a file name. If you have to deal with - the beginning of the number of file names, there are two ways:
touch ./-hello
or
touch -- -hello
\ there is a usage in the \ representation knockout round after line continuation, Shell does not execute the command immediately, but to move to the next his party, given a continued prompt>, continue to wait for user input, and finally all the continuation lines received as a command executed together. For example:
ls \
> -l
@experience: more than two lines is equivalent to the output of the ls -l command, only one line written no fewer than two lines to write, this is a very frequently used, for example, you call java to write the job with a shell, inside the parameters particularly long you can not write it on one line, this time will use this thing

apostrophe

Single quote ( press enter echo in single quotes will take effect when the carriage return )
and C language is not the same, Shell script single and double quotes are the same as the string delimiter), rather than character delimiters. Single quotes are used to keep the literal value of all characters within the quotes, even \ and carriage returns within the quotation marks is no exception, but the string can not appear in a single quote . If the quote is not paired to enter a carriage return, Shell will continue to give prompt asking the user to put quotes accompanied by pairs. For example:
echo '$SHELL'
$ SHELL
echo 'ABC\(回车)
> DE'(再按一次回车结束命令)
ABC
DE

Double quotes

Double quotation marks are used to enclose the content will be treated as a single string. It prevents wildcard expansion but allow variable expansion. This is a different approach with single quotes
DATE=$(date)
echo "$DATE"
echo '$DATE'
Here Insert Picture Description
so that you know at a glance the difference between double quotes with single quotes

Published 25 original articles · won praise 25 · views 2249

Guess you like

Origin blog.csdn.net/weixin_43071838/article/details/104529485