Bash – Copy command from history to bash prompt

bashcommand historykeyboardshell-script

Sometimes I'd like to get some command from history output, copy it to a bash prompt, make some changes and run it. Is there a way to copy a command from history output to a bash prompt without involving the mouse? For example, it would be some script that I can bind a shortcut to.

Maybe I do not understand fc enough, but it looks like I cannot select folders or files via fc (like Tab for usual command prompt)

Best Answer

you need to take a look at the bash fc command.

If you want to edit before rexecuting a history line just do like this

fc 123

where 123 is the history line number you see typing the command history.
It will open your favorite editor and allow you to modifiy the line then quit and save and it will run.

You can also do it a range of command like this:

fc 123 135

To work on command history from 123 to 135.

Edit 1:
if you need to run without edit consider watch the post Re-execute fc command from history

Related Question