I am using make
from inside MSYS2 project, in general without problems. However, if I use zsh, I am unable to switch subsystems. For example:
source shell mingw64
gives:
/usr/bin/shell:58: bad substitution
Clearly, there is bash specific code in the shell
script and the script is sourced because it sets environment variable in the calling shell.
One could fix this amending shell
code, but that could be overwritten or become incompatible, after the next pacman -Syu
.
Is there a general solution to source Bash scripts in zsh (or a solution specific for switching MSYS subsystem)?
You can't interpret arbitrary bash scripts in zsh, but you can start a new copy of bash with directions to source a script and then hand over control to a zsh interpreter:
bash -c 'set -a; source shell mingw64 && exec zsh -i'
That zsh interpreter will thus inherit exported environment variables and working directory changes made by sourcing the bash script; it will not inherit shell-local (non-exported) variables, aliases or functions.
set -a
instructs bash to export all variables by default, thus ensuring that variables set by your sourced script are whenever possible placed in the environment rather than kept shell-local. This won't work for values of types that can't be exported (such as arrays), but is a reasonable interim measure.
By the way -- there's an upstream ticket calling for this code to be made compatible with /bin/sh
. Should that happen, zsh will be able to interpret it when in POSIX-compatibility mode, which you can temporarily enter as follows:
emulate sh -c 'source shell mingw64'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With