I have a block of text that I want to cat into bash and then send to python so I can work with it more.

So my command line is cat input_file | sh bash_file.sh

My sh file is

"$input_data" | python3 ./python_file.py

and my python file is

contents = sys.stdin.read()

But nothing is actually getting stored to contents. How can I print out what I have catted into my shell script file from python?


You've got it mostly right, but you're not printing the contents of $input_data to stdin. Instead you're trying to run a command named $input_data.

Use printf to print the contents of the variable by changing the last line of your shell script like this:

printf "%s" "$input_data" | python3 ./python_file.py
  • 1
    Shouldn't that be printf '%s' "$input_data"? (I guess the original script could just as well simply use cat | ... without the variable anyway, though.) – DonHolgo Nov 25 '20 at 9:49
  • Good point, @DonHolgo. Without using %s the shell will interpret sequences like \n in the text instead of printing them verbatim. I've updated my answer. (As for cat | ..., I assume the OP is actually doing some additional processing to populate $input_data. – satwell Nov 25 '20 at 14:54
  • cat | ... does not work because it does not quit and sys.stdin.read() waits for EOF. But you could use python3 ... <<< "$input_data". – pLumo Nov 25 '20 at 14:59
  • @pLumo If cat doesn't quit, do we even get past the assignment line? – DonHolgo Nov 25 '20 at 15:34
  • Sure, with Ctrl+d – pLumo Nov 25 '20 at 16:06

The standard input of your shell script is passed to the python command you have into the script, similarly to the way the standard output of your python script is going to the stdout of the shell script. So, there is no need to use a variable, this is enough:

python python_file.py

and you can call it either with your cat file | sh script.sh or better

sh script.sh < file

If you need more readability, you could use cat - | python script.py, where - means stdin, or add a comment saying that this execution waits for the standard input.

  • 1
    Good answer. A simple and clean solution. – glenn jackman Nov 26 '20 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.