By pre-"typed", I mean the interactive console has code text waiting for the user to (edit and) run by simply pressing enter.

It looks like something readline should support, but a confirmation that it doesn't is good enough. At least I will know that installing an additional automation tool (like expect) is the only way.

  • 1
    Are you referring to using the readline library in an application of your own, or simply using a shell that makes use of readline for interactive input (such as bash's read -ei "initial text")? – steeldriver Oct 4 '15 at 21:35
  • 2
    Exactly what read -ei does. – glarry Oct 4 '15 at 22:02
  • 1
    Why not just add it to the history list? Then you just need to type up-enter. – Wildcard Oct 4 '15 at 23:16

Not sure if this helps, and is not per readline, but if python is an alternative (or some similar) one approach could be in the direction of:

#!/usr/bin/env python
""" Inject command to own command line """

import sys, fcntl, termios

def main():
    """ x """
    tty = sys.stdin
    old_attr = termios.tcgetattr(tty)
    new_attr = termios.tcgetattr(tty)
    # No echo please
    new_attr[3] &= ~termios.ECHO
    termios.tcsetattr(tty, termios.TCSANOW, new_attr)

    cmd = ' '.join(sys.argv[1:])
    for char in cmd:
        fcntl.ioctl(tty, termios.TIOCSTI, char)

    termios.tcsetattr(tty, termios.TCSANOW, old_attr)

if __name__ == '__main__':

As in:

 script_name command to inject
  • It helps, and readline alternatives are welcome, especially if widely available on reasonably minimal systems. It fully deserved my upvote and it will become the accepted answer if I won't find any more basic option. – glarry Oct 5 '15 at 7:23
  • Just marked as accepted. Sorry for the delay. I completely forgot about it. – glarry Jan 4 '17 at 11:18

Under ZShell, the print -z shell builtin can accomplish this.

% echo 'print -z "ls -l"' >> ~/.zshrc
% exec zsh
% ls -l

ZSH otherwise does not use readline, so any readline solution would doubtless "not work"™ for ZSH.

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.