0

I just learned about the fc builtin to bash. I want to use it to edit and execute the previous two commands, including the most recent command.

I can edit-and-execute two commands up to but not including the most recent command with fc -2 0. This is what I want, except offset by one to include the most recent command.

One workable but annoying solution is to examine the output of history (or fc -l), find the absolute command numbers, and give the command range in absolute terms. For example, if fc -l -2 outputs the following:

502  echo hello
503  echo goodbye

…then I could say fc 502 503. This is what I want, except that I don't want the extra step of examining history output. (I know, I'm picky.)

Is there any way to do this that doesn't involve (1) manual examination of the history or (2) a throwaway command to bump the commands of interest out of the most recent spot?

I'm running GNU bash version 3.2.57 on macOS, for what it's worth.

  • Does fc -s -2; fc -s -1 work for you? It works on bash in Ubuntu 16.04. – AFH Oct 17 '17 at 21:09
  • @AFH - Alas, no. The -s skips the editing step. (Plus I'd like to edit the two lines together.) Almost though. :-) – Jeff Terrell Ph.D. Oct 17 '17 at 21:22
  • If you want to edit and execute, then miss out the -s, as in fc -2; fc -1. – AFH Oct 17 '17 at 21:33
  • Right, but I want to edit the two lines together. fc -2; fc -1 edits and executes serially. Furthermore, for whatever reason, fc -2; fc -1 edits the same command (number -2) twice, rather than the two most recent ones. – Jeff Terrell Ph.D. Oct 17 '17 at 21:52
  • 1
    I get the same command twice only if I abort the first edit, but I can't work out why. It's not clear exactly what you want. Do you want to edit two commands in one edit? If this is what you want, then fc -- -2 -1 appears to do this, as -- makes all the following parameters positional, not options. – AFH Oct 17 '17 at 23:16
1

One would expect fc -2 -1 to work, but there seems to be confusion between options and positional parameters which begin with -. However, using the standard -- to separate options from parameters appears to offer a solution:

fc -- -2 -1

Note that versions of bash prior to 4.3. had other problems with the fc command, including fff. here, where the history line selection can be wrong.

0

This takes the id of the latest 2 commands and run them again

fc -l -2 | cut -f1 | fc -s
  • Unfortunately, that doesn't let me edit the commands before executing them. Thanks though! – Jeff Terrell Ph.D. Oct 18 '17 at 19:51
  • I am afraid I don't understand what you bean by editing the command before executing them. Could you please be more clear about your edit requirements? E.g. you want to automatically open a text file with the latest 2 commands, edit it, save it, and run the commands in the text file. – Igor Fobia Oct 19 '17 at 8:57
  • Yes, that's exactly it. It's what the fc builtin does unless the -l or -s switch is specified. – Jeff Terrell Ph.D. Oct 19 '17 at 12:58
  • then the comment of @AFH to your question seems the right answer to me – Igor Fobia Oct 19 '17 at 13:15

Your Answer

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

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