windowssetx

setx in Windows 10 removes backslashes


I'm trying to set my HOME path in Windows 10:

setx -m HOME C:\Users\MyName

Then I check the variable's value:

echo %HOME%

and it returns

c:UsersMyName

This is a pain, since I'm trying to use Git with ssh and it needs to know the home folder where my .ssh folder is stored. What am I doing wrong?


Solution

  • Probably you're calling setx from bash. Since \ is an escape character in bash and most other Unix shells, it won't result in a literal backslash. You need to escape the backslash itself with \ or quote it with ':

    $ echo setx -m HOME C:\Users\MyName
    setx -m HOME C:UsersMyName
    $ echo setx -m HOME 'C:\Users\MyName'
    setx -m HOME C:\Users\MyName
    $ echo setx -m HOME C:\\Users\\MyName
    setx -m HOME C:\Users\MyName
    

    Or just call setx from a Windows shell like cmd or powershell. But as I said, if you need to call setx from a script every time you run it then you're doing it wrong. In that case you need to use a normal set:

    set "HOME=%USERPROFILE%"