Home Linux Commands The fc Command Tutorial With Examples For Beginners

The fc Command Tutorial With Examples For Beginners

Display or execute commands from the history list using fc command in Linux

By sk
Published: Last Updated on 3.4K views

The fc command, short for fix commands, is a shell built-in command used to list, edit and re-execute the most recently entered commands into an interactive shell. You can edit the recently entered commands in your favorite editor and run them without having to retype the entire commands.

The fc command can be helpful to correct the spelling mistakes in the previously entered commands and avoids the repetition of long and complicated commands. Since it is shell-builtin, it is available in most shells, including Bash, Zsh, Ksh etc. In this brief tutorial, we are going to learn to use fc command in Linux.

List Recently Executed Commands with fc Command

If you run fc -l command with no arguments, it will display the last 16 commands.

$ fc -l
507 fish
508 fc -l
509 sudo netctl restart wlp9s0sktab
510 ls -l
511 pwd
512 uname -r
513 uname -ms
514 touch ostechnix.txt
515 vi ostechnix.txt
516 echo "Welcome to OSTechNix"
517 sudo apcman -Syu
518 sudo pacman -Syu
519 more ostechnix.txt 
520 wc -l ostechnix.txt 
521 cat ostechnix.txt 
522 clear

List Recent Commands in Reverse Order

To reverse the order of the commands (newest commands listed first), use -r flag.

$ fc -r

List Recent Commands without Line Numbers

As you may have noticed, all the previously executed commands are displayed with prefix numbers. You can suppress the line numbers using -n flag.

$ fc -ln
 nano ~/.profile
 source ~/.profile
 source ~/.profile
 fc -ln
 fc -l
 sudo netctl restart wlp9s0sktab
 ls -l
 pwd
 uname -r
 uname -ms
 echo "Welcome to OSTechNix"
 sudo apcman -Syu
 cat ostechnix.txt 
 wc -l ostechnix.txt 
 more ostechnix.txt 
 clear

Now you won't see the line numbers.

Display Result Starting from a Specific Command

To list the result starting from a specific command, simply use the line number along with -l option. For instance, to display the commands starting from line number 520 up to the present, we do:

$ fc -l 520
520 ls -l
521 pwd
522 uname -r
523 uname -ms
524 echo "Welcome to OSTechNix"
525 sudo apcman -Syu
526 cat ostechnix.txt 
527 wc -l ostechnix.txt 
528 more ostechnix.txt 
529 clear
530 fc -ln
531 fc -l

Instead of using the line numbers, we can also use strings. For example, list the commands starting from pwd command up to the recent, just use the starting letter of that command (i.e. p) like below.

$ fc -l p
521 pwd
522 uname -r
523 uname -ms
524 echo "Welcome to OSTechNix"
525 sudo apcman -Syu
526 cat ostechnix.txt 
527 wc -l ostechnix.txt 
528 more ostechnix.txt 
529 clear
530 fc -ln
531 fc -l
532 fc -l 520
533 fc -l 520 525
534 fc -l 520
535 fc -l 522
536 fc -l l

List Recent Commands within a Specific Range

To list a commands within a specific range, for example 520 to 525, do:

$ fc -l 520 525
520 ls -l
521 pwd
522 uname -r
523 uname -ms
524 echo "Welcome to OSTechNix"
525 sudo apcman -Syu

List Commands Between Specific Range

To see everything between pwd to more command, you could use either:

$ fc -l p m

Or, use combination of first letter of the starting command command and line number of the ending command:

$ fc -l p 528

Or, just line numbers of starting and ending commands:

$ fc -l 521 528

All of these three commands will display the same result.

Edit and Re-run the Last Command Automatically

At times, you might have misspelled a previous command. In such situations, you can easily edit the spelling mistakes of the command using your default editor and execute it without having to retype again.

To edit the last command and re-run it again, do:

$ fc

This will open your last command in the default editor.

Edit and re-run the last command using fc command
Edit and re-run the last command using fc command

As you see in the above screenshot, my last command was "fc -l". Now you can make any changes in the command and re-run it automatically again once you save and quit the editor. This can be useful when you use long and complicated commands or arguments.

Important note: Please be mindful that this also can be a destructive. For example, if the previous command was a deadly command like "rm -fr <some-path>", it will automatically execute and you may lost your important data. So, you MUST be very careful while using fc command.

Change the Default Editor to Edit Commands

Another notable option of fc is "e" that is used to choose a different editor to edit the commands. For example, we can use "nano" editor to edit the last command like below.

$ fc -e nano

This command will open the nano editor(instead of the default editor) to edit last command.

Change the default editor for fc command
Change the default editor for fc command

Set Default Editor for fc Command

You may find it time consuming to use -e option for each command. To make the new editor as your default, just set the environment variable FCEDIT to the name of the editor you want fc to use.

For example, to set "nano" as the new default editor, edit your ~/.profile or environment file:

$ vi ~/.profile

Add the following line:

FCEDIT=nano

You can also use the full path of the editor like below.

FCEDIT=/usr/local/bin/emacs

Type :wq to save and close the file. To update the changes, run:

$ source ~/.profile

From now on simply type "fc" to edit the last command using "nano" editor.

Re-run the Last Command without Editing It

We already knew if we run "fc" without any arguments, it loads the default editor with the most recent command. At times, you may not want to edit, but simply execute the last command.

To do so, use hyphen (-) symbol at the end as shown below.

$ echo "Welcome to OSTechNix"
Welcome to OSTechNix

$ fc -e -
echo "Welcome to OSTechNix"
Welcome to OSTechNix

As you see, fc didn't edit the last command (i.e echo "Welcome to OSTechNix") even if I used -e option.

Please note that some of the options are shell-specific. They may not work in other shells. For example the following options can be used in zsh shell. They won't work in Bash or Ksh shells.

Display when the Commands were Executed

To view when the commands were run, use -d like below.

fc -ld
1 18:41 exit
2 18:41 clear
3 18:42 fc -l
4 18:42 sudo netctl restart wlp9s0sktab
5 18:42 ls -l
6 18:42 pwd
7 18:42 uname -r
8 18:43 uname -ms
9 18:43 cat ostechnix.txt
10 18:43 echo "Welcome to OSTechNix"
11 18:43 more ostechnix.txt
12 18:43 wc -l ostechnix.txt
13 18:43 cat ostechnix.txt
14 18:43 clear
15 18:43 fc -l

Now you see the execution time of most recently executed commands.

We can also display the full timestamp of each command using -f option.

 fc -lf
 1 4/5/2018 18:41 exit
 2 4/5/2018 18:41 clear
 3 4/5/2018 18:42 fc -l
 4 4/5/2018 18:42 sudo netctl restart wlp9s0sktab
 5 4/5/2018 18:42 ls -l
 6 4/5/2018 18:42 pwd
 7 4/5/2018 18:42 uname -r
 8 4/5/2018 18:43 uname -ms
 9 4/5/2018 18:43 cat ostechnix.txt
 10 4/5/2018 18:43 echo "Welcome to OSTechNix"
 11 4/5/2018 18:43 more ostechnix.txt
 12 4/5/2018 18:43 wc -l ostechnix.txt
 13 4/5/2018 18:43 cat ostechnix.txt
 14 4/5/2018 18:43 clear
 15 4/5/2018 18:43 fc -l
 16 4/5/2018 18:43 fc -ld

Of course, the European folks can use european date format using -E option.

 fc -lE
 2 5.4.2018 18:41 clear
 3 5.4.2018 18:42 fc -l
 4 5.4.2018 18:42 sudo netctl restart wlp9s0sktab
 5 5.4.2018 18:42 ls -l
 6 5.4.2018 18:42 pwd
 7 5.4.2018 18:42 uname -r
 8 5.4.2018 18:43 uname -ms
 9 5.4.2018 18:43 cat ostechnix.txt
 10 5.4.2018 18:43 echo "Welcome to OSTechNix"
 11 5.4.2018 18:43 more ostechnix.txt
 12 5.4.2018 18:43 wc -l ostechnix.txt
 13 5.4.2018 18:43 cat ostechnix.txt
 14 5.4.2018 18:43 clear
 15 5.4.2018 18:43 fc -l
 16 5.4.2018 18:43 fc -ld
 17 5.4.2018 18:49 fc -lf

Summary

Let me summarize the important key points.

  • When running without any arguments, fc will load the most recent command in the default text editor.
  • When running with a numeric argument, fc loads the editor with the command with that specified number.
  • When running with a string argument, fc loads the most recent command starting with that specified string.
  • When running with two arguments to fc , the arguments specify the beginning and end of a range of commands.

Note: Some options are deprecated in recent BASH versions.

Getting Help

For more details, refer the manual page of fc command.

$ man fc

In some distributions, the manual page for fc command is not available. In such cases, you can type fc --help command to display the help section.

$ fc --help
fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]
    Display or execute commands from the history list.
    
    fc is used to list or edit and re-execute commands from the history list.
    FIRST and LAST can be numbers specifying the range, or FIRST can be a
    string, which means the most recent command beginning with that
    string.
    
    Options:
      -e ENAME	select which editor to use.  Default is FCEDIT, then EDITOR,
    		then vi
      -l 	list lines instead of editing
      -n	omit line numbers when listing
      -r	reverse the order of the lines (newest listed first)
    
    With the `fc -s [pat=rep ...] [command]' format, COMMAND is
    re-executed after the substitution OLD=NEW is performed.
    
    A useful alias to use with this is r='fc -s', so that typing `r cc'
    runs the last command beginning with `cc' and typing `r' re-executes
    the last command.
    
    Exit Status:
    Returns success or status of executed command; non-zero if an error occurs.

Hope this helps.

You May Also Like

4 comments

Michael D Krailo January 19, 2021 - 9:32 pm

Interesting that my version of bash 5.0.17 does not have the -d or -f options for fc. That sucks! What version of bash are you running?

Reply
sk January 19, 2021 - 9:55 pm

It’s an old article. Now I use the same Bash version (5.0.17) here. Looks like both options are deprecated in the recent Bash version.

Reply
Pedro Alvaro August 13, 2021 - 4:39 pm

Para GNU 8.26 no acaba de funcionar del todo bien,

al realizar dentro de un bash
#!/bin/bash
valor=$(shuf -ze -n6 {A..Z} {a..z} {1..9} )
me escupe con el texto:
AVISO: command substitution: ignored null byte in input.

**************************************************************

For GNU 8.26 it doesn’t quite work quite right, when performing within a bash

#!/bin/bash

value = $ (shuf -ze -n6 {A..Z} {a..z} {1..9})

spits me out with the text:

WARNING: command substitution: ignored null byte in input.

Reply
sk August 13, 2021 - 7:47 pm

A few options are deprecated in the recent version. Refer the man pages of fc command for details.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More