Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
Heard first time this command fc. Thanks for enlightening :) –  SHW Dec 18 '13 at 8:55

2 Answers 2

up vote 4 down vote accepted
+50

Add this to your .bashrc :

fc() {
    command fc "$@"
    history -s fc $@
}
share|improve this answer
    
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 '13 at 20:37

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

share|improve this answer
    
Thanks for your answer. Only, I do not understand how it is different from the one by @Baronsed? –  Bernhard Dec 15 '13 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? –  StefanKaerst Dec 16 '13 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 '13 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 –  StefanKaerst Dec 17 '13 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 '13 at 7:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.