Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pipe command's output to program and keep its stdin open

Tags:

bash

shell

stdin

for example, I want to to send `print(32) to python and it should print 32 and remain open until I press ctrl+d

I did following

echo 'print(32)' | python

but It just prints 32 and closes the python.

I want to keep the Python's interactive shell open to send further commands from my keyboard.

EDIT: that was just example. instead of python it could be node, ruby or docker exec -it command.

basically I want to pipe output as if I was typing via my keyboard

like image 921
LightSith Avatar asked Oct 23 '25 06:10

LightSith


1 Answers

Use the -i flag to ask python to inspect interactively after running a script, combine with process substitution <(..) construct in bash. This will let the python command assume the commands are coming from a file even though it isn't

python -i <(echo 'print(32)')
32
>>>

or set the PYTHONINSPECT variable to a non-empty string to emulate the -i behavior

PYTHONINSPECT=x python <(echo 'print(32)')

Note that Process substitution is not supported in POSIX mode, but on bash/zsh or ksh93 only.

like image 186
Inian Avatar answered Oct 25 '25 03:10

Inian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!