Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable expansion and escaped characters

In PowerShell, you can expand variables within strings as shown below:

$myvar = "hello"
$myvar1 = "$myvar`world" #without the `, powershell would look for a variable called $myvarworld
Write-Host $myvar1 #prints helloworld

The problem I am having is with escaped characters like nr etc, as shown below:

$myvar3 = "$myvar`albert"
Write-Host $myvar3 #prints hellolbert as `a is an alert 

also the following doesnt work:

$myvar2 = "$myvar`frank" #doesnt work
Write-Host $myvar2 #prints hellorank.

Question: How do I combine the strings without worrying about escaped characters when I am using the automatic variable expansion featurie? Or do I have to do it only this way:

$myvar = "hello"
$myvar1 = "$myvar"+"world" #using +
Write-Host $myvar1
$myvar2 = "$myvar"+"frank" #using +
like image 954
Raj Rao Avatar asked Jul 05 '11 17:07

Raj Rao


People also ask

What are escaped variables?

Page 3. Escaping Variable. Technically escaping means “cannot be stored in a register”. In C Large values (arrays, structs). Variables whose address is taken.

What is meant by escaping characters?

In computing and telecommunication, an escape character is a character that invokes an alternative interpretation on the following characters in a character sequence. An escape character is a particular case of metacharacters.

How do you escape special characters in variable Bash?

Bash escape character is defined by non-quoted backslash (\). It preserves the literal value of the character followed by this symbol. Normally, $ symbol is used in bash to represent any defined variable.


1 Answers

This way is not yet mentioned:

"$($myvar)frank"

And this:

"${myvar}frank"
like image 130
Roman Kuzmin Avatar answered Sep 22 '22 19:09

Roman Kuzmin