Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of special characters in Shell scripts?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "What are the functions of special characters in Shell scripts?" Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "What are the functions of special characters in Shell scripts?"

Special characters in Shell are

1,$dollar sign

2.\backslash

3.`Back quotation marks

4,"double quotation marks

5、

< ,>

;,*,?, [,]

Let me give you a list of examples

1.$sign

1、echo $? The last command exit status is displayed

2、echo "$? "Same effect as above.

3、echo '$? Is it displayed as $?

4、echo \$? Does it show $?

5、echo "\$? "Is it $?

As you may have seen, the $symbol has a special meaning in double quotes. Double quotes don't work with the $symbol.

The single quotation marks can shield the special meaning of special characters, so that they can be displayed as characters themselves.

Bar can also shield the special meaning of special characters, so that special characters lose their special meaning.

II.\backslash

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

The code is as follows:

A=1234

echo \$A is displayed as $A if\is not added will be displayed as 1234

echo \`appears as`

echo \"appears in double quotes

echo \\appears as\

III.`Reverse quotation marks

The function of back quotation marks is command substitution, and the string in the back quotation marks is executed as a command. We often use it when programming with shell to assign the execution result of the system command to a variable.

The code is as follows:

A=`date`

echo $A shows not the date but the current time string

For example, there is a file A with the following contents:

ABCDEFG

1234456

abcdefg

B=`cat A|grep 234`? #Retrieves lines in file A containing string 234

echo $B will be displayed as 1234456

echo "$B" will show why?

echo "\$B" will show why? Readers try it themselves.

IV."Double quotation marks

Some special characters in the system, in order to avoid quoting these special characters are often enclosed in double quotes or single quotes, so that they do not have special meaning.

However, some special characters still have special meanings in quotation marks, and it does not work if they are enclosed in double quotation marks. The first four special characters listed in this article are also special characters in double quotes. 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 print these special characters as they are

The code is as follows:

echo """

echo "$"

echo "\"

echo "`"

This isn't what you expect, because double quotes don't work on them, so you have to print the prototype of these special characters

The code is as follows:

echo '"'

echo '$'

echo '\'

echo '`'

or

echo "\""

echo "\$"

echo "\\"

echo "\`"

will be displayed as " $ \ `

V. Other special characters

Notice that I've grouped all the special characters except for the first four, and that's because the first four special characters still have special meanings in double quotes, so I'm going to take them out separately, and if you want to print out the originals of these special characters, you can use double quotes or single quotes to make them lose their special meanings.

< ,>

;,*,?, [,] has a special meaning for shell but you can enter these prototypes in double quotes.

At this point, I believe that everyone has a deeper understanding of "what are the functions of special characters in Shell scripts". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report