[Tutor] bash heredoc and python
ThreeBlindQuarks
threesomequarks at proton.me
Thu Apr 27 17:45:13 EDT 2023
David,
I would have suggested that bashing was not appreciated on this forum but then realized the moderator would correct us by saying that of course it sometimes was and would not see it as humor.
So, I did not say that!
Back to being serious, the program you call bash is one of name "shell" programs with names like sh or ksh and quite a few others. The name bash is a it of a pun on the original creator of the UNIX shell program and is supposedly he Bourne Again SHell.
Your question is not even slightly about Python because the python interpreter has NO IDEA it was called this way. It was one of may UNIX shell features that have migrated to many other operating systems. Any program you call on the command line has a standard input and out and error that can be redirected in many ways. Think of what happens if you pipe into the Python interpreter from another program as an example as in:
cat file | python ...
echo "Some text" | python ...
or pipe the output elsewhere.
A HERE document is just a gimmick similar to the echo above so something like:
program args <<!
stuff
more stuff
!
Is used by the shell as the standard input till it sees the identifier (I used just an ! ) and then passes on everything before the identifier.
Inside your python program if you try to evaluate your command line arguments, you will never see the above so placing a dash like you asked will likely result in an error. There are ways using a command-line argument to place a short (typically one-liner) set of python commands the way you expect. Here is a silly example:
python -c "print(5+5)"
Which prints 10
And another which shows all command line arguments:
python -c "from sys import argv; print(argv)" non sense
['-c', 'non', 'sense']
Q
Sent with Proton Mail secure email.
------- Original Message -------
On Thursday, April 27th, 2023 at 4:14 PM, David Lowry-Duda <david at lowryduda.com> wrote:
> I like to use python as a simple commandline calculator. I only just
> learned that it's possible to do something like the following in bash
>
> `$ python <<EOF import math print('multiple lines') print(math.pi)`
>
> and python will happily run this. I had thought this wouldn't work, and
> that python would instead complain that there was no file with some
> amalgamated name.
>
> I discovered this by an accident, but googling shows of course that this
> isn't new or anything. I would have expected this to run with "python -
> <<EOF" instead of "python <<EOF", as the `-` indicates to read from
> stdin.
>
> So I opened up the python manpage to see about this, and saw nothing
> there (not even about what `-` is supposed to mean!). Then I went to
>
> https://docs.python.org/3/using/cmdline.html#command-line
>
> to see if I could learn more, and I don't see much there either. There
> is a somewhat cryptic remark: "In non-interactive mode, the entire input
> is parsed before it is executed."
>
> I guess python does something like: check to see if args were given, if
> not then check for standard input and then run it noninteractively? Do
> you think this is an intended behavior, or is this merely an
> undocumented implementation detail?
>
> - DLD
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list