What is the difference between single quotation marks and double quotation marks in Shell script strings
This article introduces the knowledge of "what is the difference between single quotation marks and double quotation marks in Shell script strings". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Basic knowledge of string
Strings are the most commonly used and useful data types in shell programming (apart from numbers and strings, there are no other types to use), strings can be in single quotation marks, double quotation marks, or without quotation marks. The difference between single and double quotation marks is similar to that of PHP.
Single quotation mark
The code is as follows:
Str='this is a string'
Restrictions on single quote strings:
Any character in the single quote will be output as is, and the variable in the single quote string is invalid.
Single quotation marks cannot appear in a single quote string (not even after using escape characters for single quotation marks).
Double quotation marks
The code is as follows:
Your_name='qinjx'
Str= "Hello, I know your are\" $your_name\ "!\ n"
Advantages of double quotation marks:
There can be variables in double quotation marks
Escape characters can appear in double quotation marks
Second, commonly used string correlation methods
Concatenate string
The code is as follows:
Your_name= "qinjx"
Greeting= "hello,"$your_name"!
Greeting_1= "hello, ${your_name}!"
Echo $greeting $greeting_1
Get string length
The code is as follows:
String= "abcd"
Echo ${# string} # output 4
Extract substring
The code is as follows:
String= "alibaba is a great company"
Echo ${string:1:4} # output liba
Look for substrings
The code is as follows:
String= "alibaba is a great company"
Echo `expr index "$string" is`
This is the end of the content of "what is the difference between single quotes and double quotes in Shell script strings". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!