$HOMEPATH + \ not working in GitBash

左心房为你撑大大i 提交于 2019-12-12 04:39:53

问题


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:


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\""


来源:https://stackoverflow.com/questions/29903089/homepath-not-working-in-gitbash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!