4

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

1
  • I think this is not keeping it open, but first have stdin connected to your pipe, and when the pipe closes, reopen stdin to your tty. Commented Nov 9, 2021 at 13:50

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for XY problem. I used python just as an example for my problem. please see edit
I don't think there is a one size fits all solution for this. Each tool might have its own implementation of interactive session handling
1

If you are in xwindow with xdotool install, the behaviour can be simulated with :

#!/bin/bash feed_initial_input(){ local initial_input="$1"; shift { sleep 0.3; xdotool type "$initial_input"$'\n'; } & "$@" } feed_initial_input "print(32)" python feed_initial_input "console.log('Hello world!')" node 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.