Invoke MSYS2 Shell from Command Prompt or PowerShell

jpmc26 picture jpmc26 · Nov 7, 2014 · Viewed 12.8k times · Source

Starting from Command Prompt or PowerShell (either is fine; I can figure out how to do the other given one), how can I invoke the MSYS2 shell to run a command?

More specific issue

I'm including this information to avoid an XY problem, but I strongly believe this is best solved by the answer to the above (more general) question.

In particular, I'm hoping to write a shell script to update the packages listed in Part III, Step 3 here. So I need something like:

[invoke msys2 bash] -c "pacman -Sy --needed msys2-runtime"

Calling the bash executable directly doesn't work:

C:\>C:\msys64\usr\bin\bash.exe -c "pacman -Sy --needed msys2-runtime"
/usr/bin/bash: pacman: command not found

I assume it's an environment problem. Changing PATH to include the path to MSYS2's bin directory doesn't fix the problem either. When it tries to install a package, bad things happens (nano is just an example):

C:\>bash -c "pacman -Sy --needed --noconfirm nano"
:: Synchronizing package databases...
 mingw32 is up to date
 mingw64 is up to date
 msys is up to date
resolving dependencies...
looking for conflicting packages...

Packages (1) nano-2.3.6-2

Total Installed Size:  1.81 MiB
Net Upgrade Size:      0.17 MiB

:: Proceed with installation? [Y/n]
(1/1) checking keys in keyring                                                                   [########################################################] 100%
(1/1) checking package integrity                                                                 [########################################################] 100%
(1/1) loading package files                                                                      [########################################################] 100%
(1/1) checking for file conflicts                                                                [########################################################] 100%
(1/1) checking available disk space                                                              [########################################################] 100%
(1/1) upgrading nano                                                                             [########################################################] 100%
error: command (/usr/bin/bash /usr/bin/bash -c . /tmp/alpm_shqjUE/.INSTALL; post_upgrade 2.3.6-2 2.3.2-1 ) failed to execute correctly

Trying to invoke the batch file (C:\msys64\msys2_shell.bat) opens a new shell in a new window, which leaves me not knowing whether it failed or succeeded.

So how can I get the shell into MSYS2's normal state?

Answer

Ray Donnelly picture Ray Donnelly · Nov 7, 2014

We're working hard to fix our messy update procedure, we want it to be trouble-free.

You can see some examples of how we invoke MSYS2 from a batch file when we create a new installer in:

https://github.com/Alexpux/MSYS2-packages/blob/master/msys2-installer/make-msys2-installer.bat

But as @StevenPenny suggests,

bash -l -c "pacman ..."

is the correct way.