Home Ask Login Register

Developers Planet

Your answer is one click away!

Noumenon February 2016

Meaning of ${PYTHON:-python} in shell script and how to change

I am trying to export a Mercurial repo to GitHub using hg-fast-export and Github Bash for Windows. It choked on the line from mercurial import node because mercurial doesn't support Python 3.

I installed Python 2.7 and tried shebang lines (#! /Python27/python) and also alias python='c:/Python27/python'. That worked to make python --version report 2.7, but the hg-fast-export.sh still invokes Python 3 because it contains the line


and that evaluates to Python 3.4.3.

Can you explain how to change this to use a different Python version and also what's going on with the syntax here? I couldn't really Google the meaning of ${} or :- in the shell. Comments on how likely my approach is to get this running on Windows could also be helpful.

Edit: Thanks for the explanations of :-. Since the parameter expansion was not needed, I guess the answer to my question was "You have to set PYTHON='c:/Python27/python' in the same line as the script for it to use that value." I expected it to be like PATH where you can set it independently for following lines to use.


Marc Young February 2016

This is a way to evaluate and modify text (parameter expansion). Consider this example:

$ PYTHON="/usr/bin/python --version"
$ ${PYTHON:-python}
Python 2.7.10

PYTHON is originally the path and command on how to evaluate the version.

${PYTHON:-python} evaluates and runs the former, but it was not empty so the colon dash is not needed

For a detailed breakdown see What does the colon dash ":-" mean in bash

Vlad February 2016

That syntax evaluates a variable, but provides a default:

$ foo=123
$ echo ${foo:-456}
$ echo ${bar:-456}

You could try to pass a modified $PYTHON to the script:

$ PYTHON=c:/Python27/python hg-fast-export.sh ...

Charles Duffy February 2016

The intent here is to allow an override to be passed in through the environment.

Thus, if you run at a POSIX shell:

$ PYTHON=python26 hg-fast-export ...

then in hg-fast-export will evaluate ${PYTHON:-python} to python26.

Post Status

Asked in February 2016
Viewed 3,945 times
Voted 6
Answered 3 times


Leave an answer

Quote of the day: live life