Skip to main content
updated examples
Source Link

This is short version of Stéphane Chazelas's answer for bash with hardcoded delay.

Add to .bashrc:

insert() {
  perl -le 'require "sys/ioctl.ph";
            $delay = 0.05;
            unless(fork) {
              select undef, undef, undef, $delay;
              ioctl(STDIN, &TIOCSTI, $_) for split "", join " ", @ARGV;
            }' -- "$@";
}

Make changesDescription how insert function works in Stéphane Chazelas's answer

Changes can be made available for current bash window with:

source ~/.bashrc
 

Usage exampleExample for one time-usage with prefilling user input after $:

$ insert "echo 'wait 1 sec'; sleep 1; echo done"
$ echo 'wait 1 sec'; sleep 1; echo done  # auto prefilled
 

ResultExample of usage for prefilling every user input after $:

$ insertPROMPT_COMMAND="insert "echoecho 'wait\'command 1prompt sec';text\'"
$ sleepecho 1;'command echoprompt done"text' # auto prefilled
command prompt text
$ echo 'wait 1'command sec';prompt sleeptext' 1;# echoauto doneprefilled

Description how insert function works in Stéphane Chazelas's answer

This is short version of Stéphane Chazelas's answer for bash with hardcoded delay.

Add to .bashrc:

insert() {
  perl -le 'require "sys/ioctl.ph";
            $delay = 0.05;
            unless(fork) {
              select undef, undef, undef, $delay;
              ioctl(STDIN, &TIOCSTI, $_) for split "", join " ", @ARGV;
            }' -- "$@";
}

Make changes available for current bash window:

source ~/.bashrc

Usage example:

insert "echo 'wait 1 sec'; sleep 1; echo done"

Result:

$ insert "echo 'wait 1 sec'; sleep 1; echo done"
$ echo 'wait 1 sec'; sleep 1; echo done

Description how insert function works in Stéphane Chazelas's answer

This is short version of Stéphane Chazelas's answer for bash with hardcoded delay.

Add to .bashrc:

insert() {
  perl -le 'require "sys/ioctl.ph";
            $delay = 0.05;
            unless(fork) {
              select undef, undef, undef, $delay;
              ioctl(STDIN, &TIOCSTI, $_) for split "", join " ", @ARGV;
            }' -- "$@";
}

Description how insert function works in Stéphane Chazelas's answer

Changes can be made available for current bash window with:

source ~/.bashrc
 

Example for one time-usage with prefilling user input after $:

$ insert "echo 'wait 1 sec'; sleep 1; echo done"
$ echo 'wait 1 sec'; sleep 1; echo done  # auto prefilled
 

Example of usage for prefilling every user input after $:

$ PROMPT_COMMAND="insert echo \'command prompt text\'"
$ echo 'command prompt text' # auto prefilled
command prompt text
$ echo 'command prompt text' # auto prefilled
Source Link

This is short version of Stéphane Chazelas's answer for bash with hardcoded delay.

Add to .bashrc:

insert() {
  perl -le 'require "sys/ioctl.ph";
            $delay = 0.05;
            unless(fork) {
              select undef, undef, undef, $delay;
              ioctl(STDIN, &TIOCSTI, $_) for split "", join " ", @ARGV;
            }' -- "$@";
}

Make changes available for current bash window:

source ~/.bashrc

Usage example:

insert "echo 'wait 1 sec'; sleep 1; echo done"

Result:

$ insert "echo 'wait 1 sec'; sleep 1; echo done"
$ echo 'wait 1 sec'; sleep 1; echo done

Description how insert function works in Stéphane Chazelas's answer