Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this snippet with a shebang #!/bin/sh and exec python inside 4 single quotes work?

I'm trying to understand one of the answers to this question:

Cannot pass an argument to python with "#!/usr/bin/env python"

#!/bin/sh
''''exec python -u -- "$0" ${1+"$@"} # '''

This works well, but I do not understand why it works with four ticks at the beginning of that line rather than three.

In addition, why the hash near the end of that string?

like image 991
John Schmitt Avatar asked Jul 03 '13 21:07

John Schmitt


People also ask

What does a shebang do in Python?

The shebang line in any script determines the script's ability to be executed like a standalone executable without typing python beforehand in the terminal or when double clicking it in a file manager (when configured properly).

What does #!/ bin python3 mean?

A shebang line #!/usr/bin/python3Its purpose is to define the location of the interpreter. By adding the line #!/usr/bin/python3 on the top of the script, we can run the file.py on a Unix system and automatically will understand that this is a python script. Alternative, you could run the script as python3 file.py .

Does shebang have to be first line?

The shebang is always on the first line of the file, and is composed of the characters #! followed by the path to the interpreter program. You can also specify command line options, if necessary.

What does #!/ Usr bin env mean?

As we mentioned earlier,#!/usr/bin/env bash is also a shebang line used in script files to execute commands with the Bash shell. It uses the env command to display the environment variables present in the system and then execute commands with the defined interpreter.


1 Answers

Python supports triple-quoted strings:

'''something'''

Shell supports only single-quoted strings:

'something'

By using four quotes, sh sees that as 2 empty strings, but Python sees the first three as the start of a triple-quoted string, and includes the fourth as part of the string value.

The rest of the line is then interpreted as a command by sh, but as part of a string by Python.

The # then forms a comment as far as sh is concerned, but it is still a string to Python, closing it off with a closing triple-quote.

So, to summarize:

  • sh sees: empty string ('') - empty string ('') - command (exec python -u -- "$0" ${1+"$@"}) - comment (# ''')
  • Python sees: triple-quoted string literal (containing the characters 'exec python -u -- "$0" ${1+"$@"} #)

So sh executes that command, replacing itself with the python -u -- with the script name and the rest of the command line arguments, and Python reads this file and just sees an initial string literal that isn't going anywhere.

Because it is the first string literal in the file, it'll be set as the docstring for the __main__ module but that is hardly going to matter if this is the main script.

like image 68
Martijn Pieters Avatar answered Oct 05 '22 12:10

Martijn Pieters