Can someone explain how this bash script works? The part I don’t understand is """:"
, what does this syntax mean in bash?
#!/bin/sh """:" echo called by bash exec python $0 ${1+"$@"} """ import sys print 'called by python, args:',sys.argv[1:]
test running result:
$ ./callself.sh xx called by bash called by python, args: ['xx'] $ ./callself.sh called by bash called by python, args: []
Advertisement
Answer
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.