Search code examples
msys2

How to concatenate paths in msys


In a bash shell script I have to set PYTHONPATH. We have the following code:

if [ ${OS} == Windows_NT ]; then
    PYTHONPATH="${TEST_DIR}input;${TEST_DIR}"
else
    PYTHONPATH="${TEST_DIR}input:${TEST_DIR}"
fi
export PYTHONPATH

This also works under msys2 as long as TEST_DIR is an absolute path. However, in my code TEST_DIR can be both an absolute and a relative path. When for example TEST_DIR=. msys2 does not perform a magic : to ; conversion, and as a result PYTHONPATH is not correctly set.

Underlying details

When msys2 exports a environment variable like /c/first/path:/usr/local it transfroms that to external non-msys program to an Windows environment variable such as c:/first/path;c:/msys2/usr/local. In that process it converts the unix style file names to Windows style file names, and transforms the : to a ; -- which is needed as a : is a common part of a Windows file name. However, this seems not to happen when msys2 does not recognize the environment variable as a path, as seems the case with ./:./input.


Solution

  • As suggested by David Grayson the code as used under Linux also works under MSYS2 and there is no need to ever use a ; as a path separator. So the correct code to use both under Windows and linux is:

    PYTHONPATH="${TEST_DIR}input:${TEST_DIR}"
    export PYTHONPATH