0

I came across this python(.py) file but it has SHELL interpreter in the first line but subsequent lines are python code in it.

It's clear that it is python file, but why is the first line has SHELL shebang in it.

If this is a SHELL script, why is the file has the extentsion .py

If this is SHELL script, how does the below code interpreted by SHELL.

#!/bin/sh
''''exec python -u -- "$0" ${1+"$@"} # '''
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import json
import os

2 Answers 2

2

That's done like, look at the following more expanded version of the script:

#!/bin/sh
''''echo $0 "is called. Hello shell world: PARS:" ${1+"$@"} #'''
''''exec python -u -- "$0" ${1+"$@"} # '''
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import json
import os
import sys

print "Hello python world", sys.argv

Shrtoly: Firstly the shell takes over the first few lines and then calls the python interpreter with itself.

The stuff after '''' is ignored by the python interpeter as being a multiline comment: Is there a way to create multiline comments in Python? so they are picked up by the shell instead, and the shell gives over the control to the python interpreter after the line ''''exec python -u -- "$0" ${1+"$@"} # ''' so the python picks up the script that comes in in $0 (which now we have printed) and with all its arguments as per ${1+"$@"} that gives a list of the arguments passed in.

From this point on it's up to python to do whatever he wants with the script.

Sign up to request clarification or add additional context in comments.

2 Comments

Why do you need four ' at the start of the line, but only three at the end? Is there something I am missing?
@FlyingTeller the four ' will evaulate to two '' ie. two empty strings, that will be joined with the echo, thus giving nothing before echo (try to run ''''echo ABC and 'X'echo ABC and ''echo ABC, see which works and which does not). If you would have three that would be an empty string '' and another string: 'echo .... . The part which is after the # is ignored by the shell script, since it's a comment, so the bash does not see the ''' at the end.
0

it's just for developers to note it has combination of shell and python, they are running it as a python file

5 Comments

This is pretty confusing, I'd create a .sh file and make it call the .py file there. Also a post that explains the bash bit: stackoverflow.com/q/52785232/9824103
@hecohaf944 - The line 2 has 4 single quotes at the beginning and 3 single quotes at the end (''''exec python -u -- "$0" ${1+"$@"} # '''). is this the way to include python code in Shell script?
What makes you so certain that it is run as a python file? Is there any evidence for that?
@intechops6 yes, you can try it out in your bash terminal: exec python -u "filename". Your like uses $0, which willl replace filename. Everything that comes after $0 are arguments, in this case, nothing. Python will also think that the shell line is a comment.
@sleepystar96 got it now. reading the filename as $0 and arguments inside the script. will execute in the terminal to understand it better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.