1

Hello guys i wanna write a Shell script that runs Python code saved in variable called $code.

So i save the script in variable $code with this command:

$ export CODE='print("Hello world")'

To resolve the problem I write the following script in a file called run:

#!/bin/bash
echo "$CODE" > main.py
python3 main.py

To running the shell script i use:

./run

and its work but I found another answer which I don't understand:

python3 <<< $CODE

so what do we mean by using <<<?

  • 1
    @user206904, you're thinking of Python's interactive prompt `>>>`. This is something different. – ChrisGPT was on strike Nov 28 '22 at 12:19
  • OP, see https://unix.stackexchange.com/q/159513/12606. Note that this has nothing to do with Python, or even programming. It's a shell thing. – ChrisGPT was on strike Nov 28 '22 at 12:21
  • I wouldn't recommend this technique. `python3 main.py` leaves your script free to read from standard input; `python3 <<< $PYCODE` does not, since the script itself is occupying standard input. – chepner Nov 28 '22 at 13:07
  • 1
    Consider using `python3 -c "$PYCODE"` instead. – chepner Nov 28 '22 at 13:07
  • ...the only reason I can think of to _not_ use `python3 -c "$PYCODE"` is the argument length limit, but you shouldn't be passing around so much data as to risk hitting that limit as a variable in the first place. – Charles Duffy Nov 28 '22 at 23:04
  • That's an OS limitation, and one that I don't think is relevant under Linux? – chepner Nov 30 '22 at 16:49

2 Answers2

1

In a lot of shells <<< denotes a here string and is a way to pass standard input to commands. <<< is used for strings, e.g.

$ python3 <<< 'print("hi there")'
hi there

It passes the word on the right to the standard input of the command on the left.

whereas << denotes a here document, e.g.

command <<MultiLineDoc 
Standard Input
That
    Streches many
Lines and preserves 
     indentation and 

linebreaks

which is useful for passing many arguments to a command, 
e.g. passing text to a program and preserving its indentation.
The beginning and ending _MultiLineDoc_ delimiter can be named any way wanted, 
it can be considered the name of the document. 
Important is that it repeats identically at 
both beginning and end and nowhere else in the 
document, everything between that delimiter is passed.
MultiLineDoc

< is used for passing the contents of a file, e.g. command < filename.txt

As for your example with <<< : You could do the same with | but that's only OK if all your variables are defined in what you are passing. If you do have other variables that you have defined in your environment and which you wish to cross reference you would use a here-string as in your example, that lets you reference other variables within the content you are passing.

Please see: https://en.wikipedia.org/wiki/Here_document

https://linuxhint.com/bash-heredoc-tutorial/

alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
0

In Bash, zsh (and some other shells) <<< is the here string operator. The code you’ve posted is roughly equivalent to

echo "$PYCODE" | python3
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Note that `echo -n` is allowed by POSIX to emit `-n` on output, and on some systems (and some configurations of bash; try `shopt -s xpg_echo; set -o posix; echo -n hi`) actually does. `printf %s "$PYCODE"` is the more portable way to write the same thing. – Charles Duffy Nov 28 '22 at 23:06
  • 1
    Also, herestrings add a trailing newline, so content-wise it's more like `printf '%s\n' "$PYCODE"` (or, with bash's default `echo` behavior, `echo "$PYCODE" |`) regardless. Note how `wc -c << – Charles Duffy Nov 28 '22 at 23:07
  • @CharlesDuffy Yes, I would never use this in portable code (I would use `printf` or here strings, depending on the shell). I just thought that `echo` might be more generally understood in this context. And you are right about the trailing newline! I keep forgetting that it adds this (and I frequently miscount my lines as a consequence). – Konrad Rudolph Nov 29 '22 at 08:41