A method for directly outputting escape characters or variable names in PowerShell _powershell

Source: Internet
Author: User

This article describes how to prevent a character in a string from being escaped or a partial character being treated as a variable in a PowerShell string, so that all characters in the string are exported as they are.

In PowerShell, an escape character is a special character that begins with a character (·) and can be used to implement functions such as wrapping, tab, and so on. The variable begins with a symbol ($) and represents a value. We have introduced the escape character in the previous article, and we have also described the inclusion of variables in the string. Interested friends can go and understand.

You can use a pair of double quotes in a PowerShell to cause a string, or a pair of single quotes to cause a string. One of the biggest differences between single and double quotes is that double quotes cause a string to be called an extensible string, whereas single quotes result in strings called literal strings. A string that is enclosed in single quotes, whether the escape character is present or the variable is present, is ignored, and everything is shown literally.

Copy Code code as follows:

PS c:\users\spaybow> $p = "PowerShell"
PS c:\users\spaybow> $str = "Hello ' t$p"
PS c:\users\spaybow> $str 1= ' Hello ' t$p '
PS c:\users\spaybow> $str
Hello PowerShell
PS c:\users\spaybow> $str 1
Hello ' t$p

The example above clearly shows the difference between single and double quotes. ' t is an escape string that represents the TAB key, $p is a string, and the value is PowerShell.
When you use double quotes, the result of the $str is "Hello PowerShell", and when you use single quotes, the output is "Hello ' t$p."

By using single quotes to assign a value to a string, we can prevent these uncertainties, such as escaping characters or variables, from being included in the string, resulting in a fixed string.

About the PowerShell in the forbidden string contains escape characters or variables, this article on the introduction of so many, I hope to help you, thank you!

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.