Sprose
Sprose

Reputation: 1345

How can I use a variable in a powershell script run in a shell command?

I have a shell script that runs a powershell command (on windows).

newPassword="New Value"
powershell -command "sed -i '/password/s/>[^<]*</>$newPassword</' C:/Users/username/.m2/settings.xml"

I can echo $newPassword and see the value 'New Value'

I can also run the below script and the password tag gets updated with 'Hello World'

powershell -command "sed -i '/password/s/>[^<]*</>Hello World</' C:/Users/username/.m2/settings.xml"

However it doesn't update the file by passing in $newPassword. What is the correct way to do this?

Upvotes: 0

Views: 424

Answers (1)

Sprose
Sprose

Reputation: 1345

Turns out I just had to add curly braces around my variable

powershell -command "sed -i '/password/s/>[^<]*</>{$newPassword}</' C:/Users/username/.m2/settings.xml"

Upvotes: 1

Related Questions