1

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

PYTHON=${PYTHON:-python}

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.

Noumenon
  • 5,099
  • 4
  • 53
  • 73
  • Possible duplicate of [Usage of :- in bash](http://stackoverflow.com/questions/10390406/usage-of-in-bash) – arco444 Feb 08 '16 at 16:36
  • 1
    @arco444, that's true as far as it goes, but I'm not sure it addresses the "how to change", for folks who aren't familiar with environment variable usage. – Charles Duffy Feb 08 '16 at 16:37
  • 1
    Aside: I'd create a symlink in `/usr/local/bin`, or another location in the PATH, named `python2`; once you've tested it to work, you can just run `PYTHON=python2 hg-fast-export ...`, and not need to worry about the difference between Windows paths, msys paths, cygwin paths, etc. – Charles Duffy Feb 08 '16 at 18:19

3 Answers3

4

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
3

That syntax evaluates a variable, but provides a default:

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

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

$ PYTHON=c:/Python27/python hg-fast-export.sh ...
Vlad
  • 18,195
  • 4
  • 41
  • 71
  • Was that last snippet in your original answer? I didn't see it at first (hence my own), and don't know if you answered inside the edit window to have it automatically folded in, or if I just misread. – Charles Duffy Feb 08 '16 at 16:38
  • I think you added yours while I was still editing mine. :) I initially wanted to answer the part of the question pertaining to accessing vars, and then I realised there might be a workaround the OP could try. You're right, it was an edit, I didn't realise there's a window during which it's not considered one. – Vlad Feb 08 '16 at 16:40
  • *nod* -- unless someone comments (might also be that upvotes/downvotes count as well, I'd have to find the docs), edits inside a few-minute period of a prior post or edit commit (two? three? five?) are folded in. – Charles Duffy Feb 08 '16 at 18:17
2

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

Community
  • 1
  • 1
Marc Young
  • 3,854
  • 3
  • 18
  • 22