$HOMEPATH + \ not working in GitBash

2019-09-11 00:46发布

I'm trying to create a script that can be used by all by all users.

I am using GitBash on a Windows 7 machine and the line which I am trying to automate is

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

Now the issue with this is,

echo &HOMEPATH
\Users\<username>

GitBash, when executing removes the \ as it's a special charecter, so when I try to run the command "proxyon", it error's with

sh.exe": Users<username>/.proxy/proxy-switch.sh: no such file or directory found

Is there any way around this? As I can't change the $HOMEPATH, as it has a unique identifier, so it couldn't be a universal script.

Any help would be appriciated.

1条回答
我想做一个坏孩纸
2楼-- · 2019-09-11 01:03

The problem here is that the value of that variable is being expanded at the time the proxyon alias is being created and then the literal string with the backslashes is being unescaped again when the alias runs.

You need to prevent one of those unescapes from happening.

If you want the value of $HOMEPATH that exists when proxyon is executed to be used (as opposed to the value of $HOMEPATH that exists when the alias is created) then switch the double quotes to single quotes on the alias creation.

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

You should quote the variable expansion when used anyway so this should really be:

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

If you want the value of $HOMEPATH that exists when the alias is created to be used (as opposed to the value of $HOMEPATH that exists when the alias is run) then you want to add escaped double quotes to the alias creation.

alias proxyon="source \"$HOMEPATH/.proxy/proxy-switch.sh\""
查看更多
登录 后发表回答