Ján Vorčák Ján Vorčák - 1 month ago 7
Linux Question

How to prefill command line input

I'm running a bash script and I'd like to prefill a command line with some command after executing the script. The only condition is that the script mustn't be running at that time.

What I need is to ...

  1. run the script

  2. have prefilled text in my command line AFTER the script has been stopped

Is it even possible? All what I tried is to simulate a bash script using

read -e -i "$comm" -p "[$USER@$HOSTNAME $PWD]$ " input
command $input

But I'm looking for something more straightforward.


You need to use the TIOCSTI ioctl. Here's an example C program that shows how it works:

#include <sys/ioctl.h>

    char buf[] = "date";
    int i;
    for (i = 0; i < sizeof buf - 1; i++)
      ioctl(0, TIOCSTI, &buf[i]);
    return 0;

Compile this and run it and "date" will be buffered as input on stdin, which your shell will read after the program exits. You can roll this up into a command that lets you stuff anything into the input stream and use that command in your bash script.