Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 917
antonio Avatar asked Nov 08 '22 10:11

antonio


1 Answers

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'
like image 179
Charles Duffy Avatar answered Nov 09 '22 23:11

Charles Duffy