Is there a way to generate arbitrary bash scripts (ie msys2-packages "shell") in zsh?

I use make from within the MSYS2 project, with no problems at all. However, if I use zsh, I cannot switch subsystems. For instance:

 source shell mingw64 

gives:

 /usr/bin/shell:58: bad substitution 

Obviously, in the shell script, bash special code exists, and the script because it sets the environment variable in the calling shell.

You can fix this shell fix code, but it may be overwritten or become incompatible after the next pacman -Syu .

Is there a general solution for source bash scripts in zsh (or a solution specific for switching MSYS subsystems)?

+5
source share
1 answer

You cannot interpret arbitrary bash scripts in zsh, but you can run a new copy of bash specifying the source for the script source, and then transfer control to the zsh interpreter:

 bash -c 'set -a; source shell mingw64 && exec zsh -i' 

Thus, the zsh interpreter inherits the exported environment variables and working directory changes created by looking up the bash script; it will not inherit local (non-export) variables, aliases or shell functions.

set -a instructs bash to export all variables by default, thereby ensuring that the variables set by your script source are placed in the environment, if possible, and not supported by shell-local. This will not work for type values ​​that cannot be exported (e.g. arrays), but is a reasonable intermediate measure.


By the way, there is an upstream ticket requiring this code to be compatible with /bin/sh . If this happens, zsh will be able to interpret it in POSIX compatibility mode, which you can temporarily enter as follows:

 emulate sh -c 'source shell mingw64' 
+1
source

Source: https://habr.com/ru/post/1275320/


All Articles