0

I'm trying to replicate the behavior of the history builtin: specifically when you do !<line # of command> and it's just replaced with the command at line #.

Let's say that I have a file with the following contents:

cd ~/some/path

I would like to be able to take the contents of that file and have that pushed into the current terminal input line as such:

$ ./put_to_input file
$ cd ~/some/path # pulled from the file, not manually typed 

Not sure if this is possible. Help would be appreciated!

Clarification:

I want to put the lines of the file into the terminal input, as if the person had typed it themselves. Similar to if you use the !n shell history substitution. https://opensource.com/article/18/6/history-command

puppydog
  • 101
  • Please specify in the question: do you want to execute all the lines of the file or do you want to execute a specific line? – ingli May 08 '20 at 17:29
  • I want to put the lines of the file into the terminal input, as if the person had typed it themselves. Similar to if you use the !n shell history substitution. https://opensource.com/article/18/6/history-command – puppydog May 08 '20 at 17:31
  • 1
  • Does it have to be a file? You can use ~/.inputrc to assign a shortcut. For example, try adding this to your ~/.inputrc file (create it if it doesn't exist): Control-P: cd ~/some/path. Then, open a new terminal and press Ctrl+P, and the text will appear on your prompt. Is that a decent workaround? – terdon May 08 '20 at 18:03
  • @MarkPlotnick that was exactly it. Was able to do it in zsh with print -z $(cat runfile). Thanks! Used it to build this: https://github.com/slin63/rich-history – puppydog May 08 '20 at 19:46

4 Answers4

1

Look into .:

$ cat input
cd /etc
pwd

$ . input
/etc
DopeGhoti
  • 76,081
  • Oh. That does work but doesn't handle the case where a user might want to edit the input before executing it, which you would be able to do if it pasted the contents into the current terminal input line. If nobody finds a way to do that as well I'll go ahead and accept this answer. – puppydog May 08 '20 at 16:45
1

You can use @DopeGhoti s solution, but append it to the ~/history file then it could be recalled, and edited.

cat input >> ~/history
^r <cmd>
^e
X Tian
  • 10,463
0

In zsh, you can do print -z "content" to do this.

I was able to create a shell function to do what I needed.

Example here:

put_to_input() {
    # Push command to current terminal input line with print -z
    print -z $(cat $HOME/runfile)
}
$ cat $HOME/runfile
echo hehe
$ put_to_input
$ echo hehe # file contents appear on input line

Original source: Can bash write to its own input stream?

puppydog
  • 101
0

In bash you can append any file content to your history:

history -r file

After that you just have to press up-arrow (same as Ctrl-p) to edit and execute the commands, or start them directly with !.

Example:

$ echo '~bin/start.sh my=complicated -command=that must be "edited" all the time' > file
$ history -r file
$ history
    1  echo '~/bin/start.sh my=complicated -command=that must be "edited" all the time' > file
    2  history -r file
    3  ~/bin/start.sh my=complicated -command=that must be "edited" all the time
    4  history
$ !3
Running...
xevior
  • 51