Linux – Bash: How to output command to next prompt for user to use

bashcommand linelinuxshell

In a bash script, I want to output a command at the end, so the user only has to press Enter after my script has ended to execute this command.

E.g. the user needs to execute a command with certain parameters. Instead of typing it themself, my script outputs the command, with parameters to their command line, then exits. At this point my script is finished. The user sees a new prompt, with the command "pre-written" to it.

This gif should give an idea of what I'm talking about:


I saw this behaviour in several programs like hstr and qfc, and I think even the standard reverse-i-search does it.

I've searched Google for everything I could think of but I cannot find an answer.

Best Answer

If you are using X, the xdotool tool might be a solution, by including in the script a command such as :

xdotool type command-line-to-display

If the new text becomes mixed with the script output, some acrobatics might be required.