Special characters in shell scripts (dollar, backslash, quotes, etc.) Introduction _linux shell

Source: Internet
Author: User

Special characters in the Shell have

1, $ dollar character
2, reverse Slash
3, ' inverted quotation mark
4, "double quotes
5, <, >;,*,?, [,]

Let me make a list of the following

One, $ symbol

1, echo $? The previous instruction exit status is displayed
2, echo "$" effect Ibid.
3, Echo ' $? ' shows $?
4, Echo \$? Show $?
5, echo "\$?" shows the $?

You may have seen the $ symbol has a special meaning in double quotes. Double quotes don't work on the $ symbol
The single quotation mark can block the special meaning of the special word Fu, so that it can be displayed as the character itself, the reverse oblique
The bar can also be shielded from the special meaning of special characters, so that special characters lose special meaning.

Second, reverse slash

The effect of a backslash is to mask the special meaning of a special symbol character so that it is still the original character.

Copy Code code as follows:

a=1234
echo \ $A shown as $a if not added \ will display as 1234
echo \ ' shows as '
echo \ "appears as double quotes
echo \ \ is displayed as \

Three, ' inverted quotes

The function of the inverted quotation mark is to replace the command, to execute the string in the inverted quotation mark as a command, and we often use the Shell programming to assign the result of the system command to a variable

Copy Code code as follows:

A= ' Date '
echo $A not showing date, but time series.
For example, the contents of a document A are as follows
ABCDEFG
1234456
Abcdefg

B= ' cat A|grep 234 '? # retrieves the row containing the string 234 in file a
Echo $B will appear as 1234456
echo "$B" will show why?
echo "\ $B" will show why? Readers try it on their own

Four, "double quotes

There are special characters in the system that are often used in double quotes or single quotes to avoid referring to these special characters so that they do not have special meaning.
However, there are some special characters that have special meanings in quotation marks, and it does not work with double quotes. The first four special characters listed in this article are in double quotes or special characters. In order to make it do not have a special meaning one is the introduction of single quotes to the second is to use the backslash to make it useless.

For example, we want to output these special characters as they are

Copy Code code as follows:

echo "" "
echo "$"
echo "\"
echo "'"

The above is not the result you expect, because double quotes don't work for them, you can only do this to output the original form of these special characters.
Copy Code code as follows:

Echo ' "'
Echo ' $ '
echo ' \ '
echo ""
Or
echo "\" "
echo "\$"
echo "\"
echo "\"

will appear as "$ \" respectively.

Five, other special characters

Everyone noticed that I put all the other special characters together except for the first four special characters, because the first four special characters have special meanings in double quotes, so separate out the special characters if you want to output these special characters, You can use double quotes or single quotes to make them lose their special meaning.

<, >;,*,?, [,] has a special meaning for the shell but you can use double quotes to enter these prototypes.

So many people have noticed that all the special characters lose their special meaning in single quotes, if you want to output special characters but can't remember those special characters in double quotes can not output the original, it is recommended that you simply use single quotes.

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.