1. Example
During the use of shell, double quotes or single quotes often enclose strings, or you can use quotes to define string variables without using quotes. Examples are as follows:
str1="testString" //Double quotesstr2='testString' //Single quotesstr3=testString //No quotes
Therefore, there are three ways to define string variables, namely double quotes, single quotes and no quotes. What is the difference between these three methods? What is the difference between single quotes and double quotes?
2. Single quotes
Single quotes define what you see in a string is what you get, that is, output the content in a single quote as it is, or describe it as what you see in a single quote, and output what you will see. Single quotes are all quotes, and contents enclosed in single quotes will not be replaced by constants or variables.
var=dablelv echo '$var'
Output:
$var
3. Double quotes
The content quoted in double quotes is not what you see. If there are commands, variables, etc. in the content, the variables and commands will be parsed out first, and then the final content will be output. A double quote is a partial quote, a constant or a constant enclosed in double quotes, and a variable will be replaced by a variable content.
var=dablelv echo '$var'
Output:
dablelv
4. No quotes
When defining a string without quotation marks, the string cannot contain whitespace characters (such as Space or Tab). Quotation marks are required. Generally, continuous strings, numbers, paths, etc. can be without quotation marks. If there are commands, variables, etc. in the content, the variables and commands will be parsed out first, and then the final content will be output.
str1="test String" str2='test String' str3=test String echo $str1 echo $str2 echo $str3
Output:
test String
test String
It can be seen that if the string contains spaces, it will not be output normally.
5. Suggestions
String constants are enclosed in single quotes. If the string contains variables, commands, etc., it is not recommended to not include quotation marks.
The above is the details of what are the differences between Shell double quotes and single quotes. For more information about Shell double quotes and single quotes, please pay attention to my other related articles!