15

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?

jpmc26
  • 28,463
  • 14
  • 94
  • 146
  • 2
    I cant test it right now, but maybe try `bash -i -c ...` or `bash -l -c ...` or `bash -i -l -c ...` – Zombo Nov 07 '14 at 21:37

1 Answers1

10

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.

jpmc26
  • 28,463
  • 14
  • 94
  • 146
Ray Donnelly
  • 3,920
  • 1
  • 19
  • 20
  • Excellent! Thank you! I have no doubt that the MSYS2 team is wokring on it, but in the mean time, I just wanted a work around other than opening a new shell manually every time. =) – jpmc26 Nov 07 '14 at 23:07
  • bash -l -c seems to work well for setting DISPLAY. but it seems to fail for exo-open. "command not found" any thoughts on how to use bash to run exo-open? – SketchBookGames Jan 12 '17 at 17:45
  • 7
    The link in the answer is dead. – Dinsdale Oct 05 '18 at 04:33
  • The github link is dead because that file has since been deleted from the repo. For reference, you can find the last known copy from this repo here: https://github.com/msys2/MSYS2-packages/blob/30c73186f5ae97d54eab9f2abbbab883c95df042/msys2-installer/make-msys2-installer.bat – Todd Walton Jul 27 '22 at 17:40