Shuman Shuman - 9 months ago 35
Bash Question

How to write a bash script which calls itself with python?

Can someone explain how this bash script works? The part I don't understand is

, what does this syntax mean in bash?



echo called by bash
exec python $0 ${1+"$@"}


import sys
print 'called by python, args:',sys.argv[1:]

test running result:

$ ./ xx
called by bash
called by python, args: ['xx']

$ ./
called by bash
called by python, args: []


That's clever! In Bash, the """:" will be expanded into only :, which is the empty command (it doesn't do anything). So, the next few lines will be executed, leading to exec. At that point, Bash ceases to exist, and the file is re-read by Python (its name is $0), and the original arguments are forwarded.

The ${1+"$@"} means: If $1 is defined, pass as arguments "$@", which are the original Bash script arguments. If $1 is not defined, meaning Bash had no arguments, the result is empty, so nothing else is passed, not even the empty string.

In Python, the """ starts a multi-line string, which includes the Bash commands, and extends up to the closing """. So Python will jump right below.