7

Sometimes I am using the fc builtin in bash (3.2 if that matters), to recall commands from the history. This also offers the possibility to edit, before execution.

For example, by typing

fc 23 27

I can re-execute command 23 till 27. But now I want to execute this specific fc 23 27 command again. How should I do that? It looks as if the executed fc is not stored in the command history, although the newly executed lines are.

1
  • 1
    Heard first time this command fc. Thanks for enlightening :)
    – SHW
    Dec 18, 2013 at 8:55

2 Answers 2

5
+50

Add this to your .bashrc :

fc() {
    command fc "$@"
    history -s fc $@
}
1
  • Nice, this works. Would be neat if there was a built-in solution instead of a function. I'll award you the bounty in case of no better answers.
    – Bernhard
    Dec 14, 2013 at 20:37
0

try adding your fc 23 27 to history and reinvoke it later:

knoedel@section60:~> bash --version
GNU bash, version 4.2.42(1)-release (x86_64-suse-linux-gnu)

knoedel@section60:~> history -s "fc 4074 4076"
knoedel@section60:~> history | tail -n 2
 4090  15/12/2013 11:31:23 > fc 4074 4076
 4091  15/12/2013 11:31:25 > history | tail
knoedel@section60:~> fc -s 4090

so fc -s 4090 actually runs fc 4074 4076 but only in this session. If you logout and login again the history command numbers change. At least as I tested it. (probably when BASH_HISTSIZE is reached)

To avoid running the wrong commands I suggest saving your commands from history to a shell script with fc -n -l id id > ~/bin/something.sh

8
  • Thanks for your answer. Only, I do not understand how it is different from the one by @Baronsed?
    – Bernhard
    Dec 15, 2013 at 11:01
  • You asked for a solution with built-in tools - that is my suggestion. I did not really understand why you asked for such a solution in the first place. Baronsed already answered your question and you can use the same solution without that bash script.I just added a bit text to explain it, ok? Dec 16, 2013 at 7:59
  • I was hoping for a solution like: fc --keepinhistory 22 34 or export HISTNOTIGNORE=fc. But, apparently that does not exist.
    – Bernhard
    Dec 16, 2013 at 8:02
  • if you want to use old commands for ever disable bash history. stackoverflow.com/questions/18663078/disable-history-in-linux otherwise new commands get added to history (except fc which is no new command but running old ones again - running the last command again is the same .. it does not get added to history) hth Dec 17, 2013 at 22:09
  • I am not sure if I understand what you are trying to say, but I do not want to disable history. I only want the fc command that I type to be added to history.
    – Bernhard
    Dec 18, 2013 at 7:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .