Explore PowerShell (15th) quotation marks and escape characters

Source: Internet
Author: User

Basic usage

Conclusion:

Single quotes only represent characters in the quotation marks in any case. That is to say, the content in single quotes does not replace variables or escape characters. In double quotation marks, variable substitution and character escape are allowed. Variable Substitution and character escape are determined by the outermost quotation marks of the command.

For more information, see examples.

Output Using Double quotation marks:Copy codeThe Code is as follows: $ a = "World" <enter>
"Hello, $ a" <enter>

The running result is:

If single quotes are used, change the command:

Copy codeThe Code is as follows: $ a = "World" <enter>
'Hello, $ a' <enter>

The output result is:

This is very simple. It is also mentioned in the previous articles in this tutorial.

Output quotation marks

In single quotes, to print single quotes, you only need to repeat single quotes. That is, two single quotes generate one single quotation mark output.

For example:Copy codeThe Code is as follows: $ a = "PowerShell" <enter>
'I'm $ a' <enter>

The running result is:

Double quotation marks can be output directly without conversion. For example:

Similarly, if you want to output single quotes in double quotes, you can print them directly without conversion. In addition, to print double quotation marks in double quotation marks, you only need to repeat the double quotation marks. That is, two double quotation marks will generate a double quotation mark output.

Example 1:

Example 2:Copy codeThe Code is as follows: $ a = "PowerShell" <enter>
"My name is $ a" ", this program said." <enter>

The running result is as follows:

Use escape characters

To output some special characters, we need to use escape characters. In PowerShell, the escape sequence is defined by backquotes, that is, the character "'" under the keyboard F1 with the same key as the wavy line "'".

Common escape strings include:

'' Single quotes
'" Double quotation marks
'0 NULL
'A Alarm
'B' Return
'F Page Jump
'N' New Line
'R Line feed
'T Horizontal Tab key (horizontal Tab)
'V Vertical Tab key (vertical Tab)

Of course, the variable name in double quotation marks is used before the variable name. This function will also be canceled.

For example, in the above example:

Copy codeThe Code is as follows: $ a = "PowerShell" <enter>
"My name is '$ a" ", this program said." <enter>

The output result is as follows:Copy codeThe Code is as follows: "My name is $ a", this program said.

Another point to note is the function of anti-quotation marks: to take over command statements.

When the command we need to enter is too long, we can use reverse quotation marks in a proper place in the command, and start another line and write the last command. Simply put, you cannot separate a word.

For example:

Get-WmiObject Win32_NetworkAdapterConfiguration | Get-Member-MemberType Methods | Format-List
The above command is very long, which is not only inconvenient for input, but also for modification and viewing. Therefore, you can use the anti-quotation marks to modify the following:

Get-WmiObject Win32_NetworkAdapterConfiguration | 'get-Member-MemberType Methods | 'format-List

You only need to press enter once after the command is completed, indicating that all command input is complete. If you use reverse quotation marks in the command, the final running result is the same.

Using Reverse quotation marks not only helps us to write more conveniently, but also facilitates command modification and viewing during script writing. In my personal opinion, especially when the back-to-back quotation marks are used after the pipeline operator, it is very helpful to clarify the command structure.

Other escape characters are used in the same way as in programming languages or other scripts. Please give it 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.