Using fc command in Linux


The fc is a command  used to list, edit and re-execute a command that was previously executed on a system. It is a shell built-in command that is present in shells including bash, ksh, zsh, etc. This command is helpful for re-executing long commands without having to rewrite them.

In this article, we will discuss the usage of fc command on a Linux system along with some examples.

How to use fc command

The syntax of using fc command on a Linux system is given below.

fc [-e ename] [-lnr] [first] [last]

Where -e ename is the name of editor that you want to use for editing, -lnr are the separate options available to use with fc command and first and the last specifies the range of command executed previously.

Example of using the fc command

In a Debian-based distro, sudo apt update && sudo apt upgrade command is used to update the local package database and upgrade to a new version of software packages.

Instead of entering the above command suppose you entered sudo apt upadte && sudo apt upgrade Since there is a spelling mistake in the command it will display the correct options to use with apt in your terminal.

Now you can edit this incorrect command using fc command. Type fc in your terminal and press enter this will open the previously execute command with the default command-line editor.

Next, edit and save the command and exit from the editor.

The modified command will automatically get executed.

How to display the list of previously executed commands in a system

You can use option -l with fc command this will display the list of the previously executed command on your system. This also displays the line number with commands.

fc -l

You can see the output of this command in the given image –

Similarly using option -n will remove the line number and -r will print the given output in the reverse order that means the last command executed will displayed first.

How to edit a set of commands using fc command

You can mention the range to edit the commands previously executed on your system. The example of editing multiple commands is given below –

fc 131 139

This will open all the commands between 131 and 139 using the default editor.

You can use option -e followed by the editor name to modify these commands to a specific editor.

Conclusion

In this article, you learn to edit and re-execute a command on a system. If you have a query then leave it in the comments below.

Leave a Comment

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