Shell common knowledge (2)

Source: Internet
Author: User

Special characters in shell include:
1. $ dollar sign
2. \ backslash
3. 'quotation marks
4. Double quotation marks
5. <,> ,*,?, [,]
The following is a one-to-one description.
1. $ symbol

1. echo $? Displays the exit status of the previous command.
2. echo "$? "Same effect
3. echo '$? 'Is $?
4. echo \ $? $?
5. echo "\ $? "Show $?
You may have seen that the $ symbol has special meaning in double quotation marks. Double quotation marks do not work for the $ symbol, but single quotation marks can block the special meaning of special characters so that they can be displayed as characters themselves, the backslash can also remove the special meanings of special characters, so that special characters do not have special meanings.
Ii. \ backslash

The backslash is used to block the special meaning of a special character so that it is the original character.

A = 1234
Echo \ $ A is displayed as $ A. If no value is added, 1234 is displayed.
Echo \ 'displayed'
Echo \ "is displayed as double quotation marks
Echo \ is displayed \
Iii. 'quotation marks

The function of anti-quotation marks is to replace commands and execute strings in the anti-quotation marks as commands. We often use shell programming to assign the execution result of system commands to a variable.
A = 'date'
Echo $ A shows the time string instead of the date.
For example, the content of file A is as follows:
ABCDEFG
1234456
Abcdefg
B = 'cat A | grep 234 '# retrieve the line containing string 234 in file
Echo $ B is displayed as 1234456
Echo "$ B" will show why?
Echo "\ $ B" will show why? Try it by yourself
Iv. Double quotation marks

There are some special characters in the system. To avoid referencing these special characters, these special characters are often caused by double quotation marks or single quotation marks, so that they do not have special meanings.
However, some special characters still have special meanings in quotation marks, which are caused by double quotation marks and do not work. The first four special characters listed in this article are in double quotation marks or special characters. In order to make it have no special meaning, one is to use single quotation marks, and the other is to use \ backslash to make it useless.
For example, we want to output these special characters as they are.
Echo """
Echo "$"
Echo "\"
Echo "'"
The above is not the expected result, because double quotation marks do not work for them, you can only output the prototype of these special characters
Echo '"'
Echo '$'
Echo '\'
Echo '''
Or
Echo "\""
Echo "\ $"
Echo "\\"
Echo "\'"
It is displayed as "$ \'
5. Other special characters

We have noticed that apart from the first four special characters, I put all the other special characters in one piece. This is because the first four special characters still have special meanings in double quotation marks, so I will explain them separately, if you want to output the prototype of these special characters, you can use double quotation marks or single quotation marks to make them lose their special meaning.

<,> ,*,?, [,] It has a special meaning for shell, but you can use double quotation marks to input these prototypes.


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.