Explore PowerShell (15) quotes and escape characters _powershell

Source: Internet
Author: User
Basic Usage

In conclusion:

Single quotes in any case represent only the characters within the quotation marks. In other words, the contents of single quotes do not make the substitution of variables and the escape of characters. In double quotes, variable substitution and character escaping are allowed. In the judgment that the variable is substituted and the character is escaped, it is determined by the outermost quotation mark of the command.

For more information, or to use an example to illustrate.

Use double quotes for output:
Copy Code code as follows:

$a = "World" <enter>
"Hello, $a" <enter>

The results of the operation are:


If you use single quotes, change the command to:

Copy Code code as follows:

$a = "World" <enter>
' Hello, $a ' <enter>


The output result is:


This is very simple, as mentioned in the previous article in this tutorial.

Output quotes

In single quotes, if you want to print single quotes, just repeat the single quotation marks, that is, two single quotes produce a single quotation mark output.

For example:
Copy Code code as follows:

$a = "PowerShell" <enter>
' I ' m $a ' <enter>

The results of the operation are:

The output of double quotes in single quotes can be directly exported without the need for conversion. Such as:

Also in double quotes, if you want to output single quotes, and do not need to convert, you can print directly. Also, if you need to print double quotes in double quotes, you only need to repeat the double quotes, that is, two double quotes produce a double quotation mark output.

Example 1:

Example 2:
Copy Code code as follows:

$a = "PowerShell" <enter>
"" "" "," "" "", "" "$a" ", this program said. <enter>

The results of the operation are as follows:




using escape characters

In order to output some special characters, we need to use the escape character, in PowerShell, the escape sequence is defined by the inverted quotation mark, which is the character "'" of the keyboard F1 with the same key as the wavy line.

The common escape strings are:

`' Single quotation mark
`" Double quotes
' 0 Null value nulls
' A Alarm
' B Backspace
' F Jump page
' N New lines
' R Line Wrap
' t Horizontal Tab key (Horizontal tab)
' V Vertical tab (Portrait tab)

Of course, using an inverted quotation mark in front of a variable name in double quotes also cancels the function of this variable substitution.

For example, in the previous example:

Copy Code code as follows:

$a = "PowerShell" <enter>
"" "", "" $a "", this program said. <enter>

The output results will be:
Copy Code code as follows:

' My name is $a ', this program said.

As shown in figure:

Another point that needs to be explained is the other role of inverted quotes: to accept command statements.

When we need to enter a command that is too long, you can use an inverted quote in the appropriate place in the command, and then write the previous command on the other line. As for the "appropriate" place mentioned above, it is simple to say that one word cannot be taken apart.

For example:

Get-wmiobject Win32_NetworkAdapterConfiguration | Get-member-membertype Methods | Format-list
The above command is very long, not only not easy to our input, nor easy to modify, view, so you can use the reverse quotation marks to modify the following:

Get-wmiobject Win32_NetworkAdapterConfiguration | ' get-member-membertype Methods | ' format-list
As shown in figure:



Simply knock a carriage return at the end of the command, indicating that all the commands have been entered, and whether or not to use inverted quotes in the command, the end result is the same.

The use of inverted quotes not only helps us to write more conveniently, but also facilitates the modification of commands when writing scripts. Personal feeling, especially the use of inverted quotes behind the pipe character, is a good way to clarify the command structure.

The use of other escape characters, in fact, is the same as in programming languages or other scripts. Please give yourselves a try.

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.