2

Creating this question based on the advice of another users advice, it is a follow-up of this.

The scenario is that I am currently trying to create a Powershell script, that a first-time user can run, which will install a bunch of programs and complete several configuration changes. One of the configuration changes is to create a file which has two shell commands (posted below), which will then be executed everytime GitBash is loaded.

The commands I'm trying to input into the .sh file are.. (With " and ' removed)

alias proxyon=source $HOMEPATH/.proxy/proxy-switch.sh on

alias proxyoff=source $HOMEPATH/.proxy/proxy-switch.sh off

I'm trying to do this using the following command

add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source "$HOMEPATH/.proxy/proxy-switch.sh on"'" 

But, the execution of the ps1 script throws an error, with the top line being

Add-Content : A positional parameter cannot be found that accepts argument '/.proxy/proxy-switch.sh'.

Now, another user told me that if i was going to use double " I would need to escape the inner quotes. The escape char in Powershell from what I can tell is ` , so I have tried the following.

add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source `"$HOMEPATH/.proxy/proxy-switch.sh on"'" 

Which gave the following

The string starting: At C:\Chef\windowsdevbox-master\test.ps1:2 char:113 + add-content "${env:homepath}.proxy\TempProxy.bat" "alias proxyon='source `"$HOMEPATH/.proxy/proxy-switch.sh on" <<<< '" is missing the terminator: '.

and I also tried

add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source `"$HOMEPATH/.proxy/proxy-switch.sh on`"'" 

Which doesn't error but, it doesn't add the contents of $HOMEPATH and only adds the following

alias proxyon='source "/.proxy/proxy-switch.sh on"'

Any suggestions on this would be greatly appriciated.

5
  • Are you expecting the literal $HOMEPATH in the file or what the environment variable expands to? My $HOMEPATH is ` which I don't think gets you the output you want. You are also using ${env:homepath}` which might be what you are looking for. Commented Apr 28, 2015 at 13:53
  • I'm expecting it to add the contents of $HOMEPATH, which in my case is the below $ echo $HOMEPATH \Users\<USERNAME> <USERNAME> is a replacement. Also, the ${env:homepath}' is a Powershell command. Where as the $HOMEPATH is a shell command, which is why I need it to be like this, as it will be run in GitBash. Commented Apr 28, 2015 at 13:57
  • Do you just need to escape that the with a backtick? `$HOMEPATH. so powershell does not try to do anything with it. I don't know Gitbash but it seems like you just need to escape it. Commented Apr 28, 2015 at 14:01
  • I'll be honest, the whole understanding around "escaping it" is new to me. Something that I was told to try in the previous question and I was just triyng to learn by doing it aka trying everything, then asking why it worked. Commented Apr 28, 2015 at 14:04
  • No problem. If we are on the right track I made an answer to that effect. Commented Apr 28, 2015 at 14:15

1 Answer 1

2

I think the issue is that you want the literal $homepath in your output file in order to let GitBash deal with it. Many ways to try and escape and deal with this but lets work with you last one you made that was almost ready.

add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source `"`$HOMEPATH/.proxy/proxy-switch.sh on`"'" 

The only thing that is different here is the backtick in front of $HOMEPATH. Since PowerShell uses the $ to declare and reference variables you need to escape it, like you have done with your quotes, to prevent PowerShell from interpeting it.

Since you do not have a variable called $HOMEPATH it is created at that time with the value of null. That is why your output from your last example looked the way it did.

Use the format operator

Another option that might have been easier on the eyes is to use the format operator. If you wanted to store it in a variable temporarily you could have this.

$content = "alias proxyon='{0}'" -f 'source "$HOMEPATH/.proxy/proxy-switch.sh on"'
add-content "${env:homepath}\.proxy\TempProxy.bat" $content

or inline it would be

add-content "${env:homepath}\.proxy\TempProxy.bat" ("alias proxyon='{0}'" -f 'source "$HOMEPATH/.proxy/proxy-switch.sh on"')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Had to make a slight change but it worked. I used "add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source "$HOMEPATH/.proxy/proxy-switch.sh`"on'"" instead. Moving the on after the first set of ' " so it gives the output "alias proxyon='source "$HOMEPATH/.proxy/proxy-switch.sh"on'" I needed to do this as when it was inputted into GitBash it was interpretting the "On" as part of the directory, and not the input it required
@AdamCormack Glad to help. You could also use the format operator to remove the need for all the backticks as well. Updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.