Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python -c vs python -<< heredoc

Tags:

I am trying to run some piece of Python code in a Bash script, so i wanted to understand what is the difference between:

#!/bin/bash #your bash code  python -c " #your py code " 

vs

python - <<DOC #your py code DOC 

I checked the web but couldn't compile the bits around the topic. Do you think one is better over the other? If you wanted to return a value from Python code block to your Bash script then is a heredoc the only way?

like image 715
Kashif Avatar asked Jun 08 '15 06:06

Kashif


2 Answers

The main flaw of using a here document is that the script's standard input will be the here document. So if you have a script which wants to process its standard input, python -c is pretty much your only option.

On the other hand, using python -c '...' ties up the single-quote for the shell's needs, so you can only use double-quoted strings in your Python script; using double-quotes instead to protect the script from the shell introduces additional problems (strings in double-quotes undergo various substitutions, whereas single-quoted strings are literal in the shell).

As an aside, notice that you probably want to single-quote the here-doc delimiter, too, otherwise the Python script is subject to similar substitutions.

python - <<'____HERE' print("""Look, we can have double quotes!""") print('And single quotes! And `back ticks`!') print("$(and what looks to the shell like process substitutions and $variables!)") ____HERE 

As an alternative, escaping the delimiter works identically, if you prefer that (python - <<\____HERE)

like image 146
tripleee Avatar answered Sep 28 '22 03:09

tripleee


If you are using bash, you can avoid heredoc problems if you apply a little bit more of boilerplate:

python <(cat <<EoF  name = input() print(f'hello, {name}!')  EoF ) 

This will let you run your embedded Python script without you giving up the standard input. The overhead is mostly the same of using cmda | cmdb. This technique is known as Process Substitution.

If want to be able to somehow validate the script, I suggest that you dump it to a temporary file:

#!/bin/bash  temp_file=$(mktemp my_generated_python_script.XXXXXX.py)  cat > $temp_file <<EoF # embedded python script EoF  python3 $temp_file && rm $temp_file 

This will keep the script if it fails to run.

like image 38
PEdroArthur Avatar answered Sep 28 '22 03:09

PEdroArthur