I have installed MinGW-w64 and MSYS2. But how do I change the HOME directory in MSYS2? So that when I type cd $home or cd ~ it goes to another directory that I defined.
And how do I write a code so that the starting directory is always where the .bat file is placed on?
In cmd I used this code:
%~d1
cd "%~p1"
call cmd
so when I open cmd on my desktop, it starts from the directory on desktop.
How can I do a similar thing with msys2?
If you would like to use your windows home folder as the home folder for MSYS2, you can edit /etc/nsswitch.conf and write:
db_home: windows
Msys2 will use windows %HOME% as it's $HOME dir. If you set %HOME% in environment variables (to the windows directory you need Msys2 to use) it will work.
In one of your shell startup scripts (e.g. ~/.bash_profile
where ~
is the default/original home directory) you can change the $HOME environment variable:
export HOME=/something/else
If you want your shell to open in that directory you might need to run cd
(with no arguments) after setting $HOME
; I have not tested it.
Create or modify an MSYS2 /etc/passwd
file. Two ways of doing this are shown below.
The following command can be run from an MSYS2 shell, and works safely whether or not the file exists and whether or not it already contains the current user:
$ grep "^${USERNAME}:" /etc/passwd >/dev/null 2>&1 || mkpasswd | grep "^${USERNAME}:" >>/etc/passwd
Next, edit /etc/passwd
, and change the relevant user's home directory field (the 6th colon-delimited field).
$ vim /etc/passwd
BONUS: It is also possible to change the MSYS2 username by editing the first field.
As desired, move current home directory content to the new home directory.
Log off, then log back in.
The /etc/passwd
edits can be done without manual editing, but this makes for a more complex command-line to paste into the MSYS2 shell, and, it might not work if the /etc/passwd
file already exists and has the username in it already:
__DIR="/path/to/home"
mkpasswd | grep "^${USERNAME}:" | \
awk -v DIR="${__DIR}" -v RS=":" -v ORS="/n" \
'NR == 6 { printf(DIR ":"); next } { printf("%s", $0) } NR < 7 { printf(":") }' - >>/etc/passwd
I've created a batch file which sets the HOME variable:
set HOME=C:\Users\%USERNAME%
C:\LocalApp\PortableGit\git-bash.exe
This allows me to put my .bash_profile in this HOME (rather than on the default network location which performs very slowly).