0

I have a script that reads a line from a file into variable foo. The line is in fact a long command that I often use as a template. I am trying to edit and re-use this long command. How can I place $foo on the command line ready to edit? I do not want to copy and paste the line, nor do I want to use any new apps that need installing such as xdotool. I only want to use the basic Linux commands if possible.

The method with partial success is to append the line to the .bash_history file and then recall it in the usual way. But to do this I need to close the terminal and re-open it (so that the latest copy of the history file is loaded to RAM).

4
  • The link @steeldriver gave works well but often seems complicated. I generally use echo $foo to place the command in the history and then recall and edit it as I see fit. Doesn't work well if you have new lines, quotes, etc. in the command.
    – doneal24
    Nov 19, 2021 at 23:09
  • 1
    1. if you're using bash, you don't have to logout and login again to reload the history file. You can use history -n. Run help history for details. 2. why not just write a script or a function using positional parameters ($1, $2, etc) instead of a hard-coded template that needs to be manually edited?
    – cas
    Nov 20, 2021 at 1:13
  • Thanks cas. History -n does exactly what I want.
    – user492570
    Nov 20, 2021 at 8:30

1 Answer 1

1

To reload bash's history file into the current shell (i.e. without logging out and logging in again), run:

history -n

From help history:

  -n   read all history lines not already read from the history file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy