Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Escaping backtick in a variable string

Pretty simple question, I don’t see it listed yet.

$a = "`4Test `!User"
$a | Out-File test.txt

Output I want:

`4Test `!User"

Output I get:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

4Test !User

>Solution :

Use single quotes instead of double quotes:

$a = '`4Test `!User'
$a

Output:

`4Test `!User

Explanation:

A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable’s value before the string is passed to the command for processing. for example:

$i = 5
"The value of $i is $i."

Output:

The value of 5 is 5.

A string enclosed in single quotation marks is a verbatim string. The
string is passed to the command exactly as you type it. No
substitution is performed. for example:

$i = 5
'The value of $i is $i.'

Output:

The value $i is $i.

for further information see: about_Quoting_Rules

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading