Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pexpect suppresses first prompt #184

Closed
nbeaver opened this issue Mar 13, 2015 · 3 comments
Closed

pexpect suppresses first prompt #184

nbeaver opened this issue Mar 13, 2015 · 3 comments
Labels

Comments

@nbeaver
Copy link

nbeaver commented Mar 13, 2015

Minimal working example:

#! /usr/bin/env python
import pexpect
child = pexpect.spawn('bash')
index = child.expect_exact("$ ")
child.interact()

This should show the bash shell prompt, but it does not.

Typing Ctrl-L will repaint the screen and show the prompt, but not all shells support Ctrl-L (e.g. dash does not), so sending that keystroke is not always a possible workaround.

First brought up here:

https://stackoverflow.com/questions/28860660/pexpect-eats-bash-prompt

@jquast jquast added the docfix label Mar 13, 2015
@jquast
Copy link
Member

jquast commented Mar 13, 2015

Not a bug, pexpect does indeed "suppress" up until interact, as this is intended, it doesn't know you would later call interact. If you want to make a PR for adding something about it to the documentation i'll accept it.

Anyway. If you want to see the prompt (and anything else) printed up until the match, just print it:

#! /usr/bin/env python
from __future__ import print_function
import pexpect
child = pexpect.spawn('bash')
index = child.expect_exact("$ ")
print(child.before + child.match, end='')
child.interact()

(Be careful about matching $, not all prompts have them. If you're going to use this for any portability, you should take care to force-set PS1 and so on, an example in the pexpect.replwrap.bash() function: https://github.com/pexpect/pexpect/blob/master/pexpect/replwrap.py#L108-113 )

@jquast
Copy link
Member

jquast commented Mar 13, 2015

it has been a very long time since i used the original tcl/expect, but i seem to recall that it implies duplicating all child program output to the parent process: it always duplicates stdout, which is why the tcl/expect version doesn't require printing before interact to "see" what was given up until interact is called

@nbeaver
Copy link
Author

nbeaver commented Mar 13, 2015

Thanks, that's exactly what I needed to know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants