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).

  • Related: [How to automatically insert a string after the prompt](https://unix.stackexchange.com/questions/391679/how-to-automatically-insert-a-string-after-the-prompt), [Put text in the bash command line buffer](https://unix.stackexchange.com/questions/82630/put-text-in-the-bash-command-line-buffer) – steeldriver Nov 19 '21 at 22:25
  • 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 '21 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 '21 at 01:13
  • Thanks cas. History -n does exactly what I want. –  Nov 20 '21 at 08:30

1 Answers1

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
cas
  • 1
  • 7
  • 119
  • 185