I am using Msys2 from PortableApps under Win 10. The answer to this question (i.e., identification and solution of the problem) may be related to the Unix/Linux part of usage, or to its "adaptation" to Windows via Msys2. I do not know it in advance (otherwise, I would likely have solved it myself!), so I had posted before (now removed) a similar question in another SE site, which reaches a different audience.

I have a shell function defined in my_fun.sh as

function myfun() { echo "MYAPP=${MYAPP}" && ${MYAPP} "$@" ; }

i.e., some_executable takes options and parameters. I can run at the command line

$ some_executable -3 input.dat output.dat

Now if I run from a shell script myscript.sh

comm="myfun -3 input.dat output.dat"
echo ${comm}
eval ${comm}

(with echo I make sure the line is what I want) I get

./myscript.sh: line 8: myfun: command not found

If I do export -f myfun and then ./myscript.sh I start getting

environment: -3: command not found

which shows the source of the problem (thanks to comment by Kamil M).

In an Ubuntu box, prior to export -f ... I get the same error as above. But export fixes the problem.

What is the cause for this? Is it related to the pure Linux/Bash use, or is parsing, shell expansion, shell inheritance, etc., under Msys2 slightly different? How do I fix this, in a way that the same script works under Msys2 and Ubuntu?

Related: https://unix.stackexchange.com/questions/444946/how-can-we-run-a-command-stored-in-a-variable


I have to

export MYAPP=some_executable

in my_fun.sh. Then the other script reads the value of the variable.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.