Bash fc Command : Easily Wield the Bash Shell Like a Pro

Published:29 April 2021 - 5 min. read

Bill Kindle Image

Bill Kindle

Read more tutorials by Bill Kindle!

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

Have you ever run a command in Bash and got an error because of a misspelled word? Or perhaps you forgot the last three commands you ran? Instead of re-writing or remembering previous commands, the Bash fc command can help make quick edits and re-execute previous commands.

In this article, you will learn how to leverage fc to edit and re-execute commands from your command history. After reading this guide, you’ll be wielding fc like the command line warrior you want to be!

Prerequisites

This article will be a tutorial. If you plan on following along step-by-step, you need a Linux computer or a Windows 10 computer with Windows Subsystem for Linux (WSL) enabled. This tutorial will be using Ubuntu 20.04 LTS.

About the Bash fc Command

fc is not a Linux application but a built-in shell utility in Bash. Bash is available in most major Linux distributions. With a few keystrokes, the Bash fc command allows you to list, edit, or execute previous commands.

The fc command is also available in Z Shell (ZSH). But the example in this article will use fc in Bash.

Did you run a long command that turned out to be wrong? fc will save you from re-typing the whole command. Instead, fc lets you edit the command using the text editor of your choice. After saving your edits, the edited command(s) will automatically execute!

Ready to take fc for a spin? Let’s dive in!

Listing Previous Commands

Have you moved away from your computer, came back, and forgot which command you ran last? Or maybe you can’t recall if you’ve executed multiple commands in the right sequence. What do you do? Relax, fc will help you remember. Here’s how.

1. Open a Bash terminal on your computer.

2. For fc to be of any good, it must have some kind of command history to work with. Since you’ve just opened a new terminal window, there may not be a command history yet. To build up a command history for testing, copy each command below and run it in your terminal. These commands are non-intrusive, and you’re not putting your computer in danger.

# Clear the screen
 clear
 
# print a string to the terminal session
 echo 'working on the command line with fc'
 
# show some system information
 uname -a
 
# use PS command to get process information
 ps aux

3. Next, list all commands in the history by running the command below. The -l option instructs fc to only list all previous commands and not execute them.

fc -l

As you can see below, the fc command displayed all four commands that you previously issued. The numbers on the left indicate the order of when you executed each command.

Listing command history with fc
Listing command history with fc

Editing and Re-Executing the Last Command

Imagine a situation where you are installing an application, and you made a spelling mistake. As a result, the command fails. Instead of re-typing the whole command, why not edit the command and automatically re-execute? To do so, follow these steps.

1. Simulate installing a non-existing application by running the command below in your terminal. Notice that you’ll intentionally misspell the application name caffiene.

sudo apt install caffiene -y

As a result, the attempted installation fails. You will get a message similar to the one below.

Failed installation message
Failed installation message

2. To correct the last command you executed, run the command below in the terminal. Doing so will open the command history buffer in the default text editor. In this example, the default text editor is nano.

fc

The screenshot below shows the last command inside the nano editor.

Editing the last command with nano.
Editing the last command with nano.

If nano isn’t your preferred text editor, you can instruct fc to use a different editor, such as vi. To do so, include the -e option followed by the text editor name. For example, fc -e vi will open the command history in the vi text editor.

3. Once you’re in the nano text editor, correct the application name spelling from caffiene to caffeine.

4. After correcting the application name, press CTRL+X to exit nano.

5. At the next prompt asking you to Save modified buffer?, press Y —> Enter to save the changes.

Editing the last command and saving changes to the history buffer
Editing the last command and saving changes to the history buffer

Immediately after exiting the text editor, fc automatically executes the command you edited.

Automatically re-executing the edited command
Automatically re-executing the edited command

Editing and Re-Executing a Previous Command

With fc, not only can you edit the last command you’ve run, but you can edit any of the previous commands you executed. For example, to edit the command echo 'working on the command line with fc', follow these steps.

1. List the previous command in the terminal by running the command below.

fc -l

2. Find the command you want to edit from the command history list and take note of the number to its left. In this example, the command number is 2.

Listing the command history and finding the command number to edit
Listing the command history and finding the command number to edit

3. To edit the command with the entry number of 2, run the command below in the terminal. The command will open in the text editor for editing.

fc 2

4. Once in the text editor, now edit the command as needed.

5. After editing the command, press CTRL+X to exit nano.

6. At the next prompt asking you to Save modified buffer?, press Y —> Enter to save the changes.

Editing a past command entry in the history buffer
Editing a past command entry in the history buffer

After saving the command and exiting the text editor, the command you edited automatically executes in the shell.

Editing and Re-Executing Multiple Commands

If you think fc can only edit and execute one previous command at a time, then you’re in for a treat. Why? fc allows you to edit multiple previous commands, too! How is editing multiple previous commands useful? For one, you can change the order of command execution.

Suppose you want to edit the previous command numbers 2 to 7. To do so, proceed as follows.

1. Run the below command in your Bash terminal and press Enter. The command below reads as fc <space> 2 <space> 7, which means that fc will open commands 2 to 7 for editing.

fc 2 7

2. Next, edit the commands as needed. For this example:

  • Delete the sudo apt install caffiene -y command.
  • Swap the order of uname -a and ps aux.
  • Delete clear.
Editing command history entries 2 through 7 at once
Editing command history entries 2 through 7 at once

3. After editing the commands, press CTRL+X to exit nano.

4. At the next prompt asking you to Save modified buffer?, press Y —> Enter to save the changes.

The commands you edited will automatically execute after exiting the text editor.

Automatically re-executing the edited command
Automatically re-executing the edited command

Re-Executing a Previous Command Without Editing

Suppose you only need to execute a previous command without editing first. fc has an option for that. Follow the steps below to execute a previous command from a specified entry in the history.

  1. List the previous command in the terminal by running the command below.
fc -l

2. From the command history list, find the command you want to run again and take note of the number to its left. In this example, the command number is 2.

Listing the command history and finding the command number to execute
Listing the command history and finding the command number to execute

3. To execute the specific previous command, run the command below in the shell. The -s option instructs fc to skip editing and continue to execute command number 2.

fc -s 2

The screenshot below shows fc executed command number 2 from the command history.

Executing a previous command from the history buffer without editing
Executing a previous command from the history buffer without editing

Conclusion

This article set out to pique your interest in a useful but often missed and forgotten command called fc. You learned how to use fc to avoid re-typing previously executed commands, which can be a waste of time.

With the knowledge that you now have about fc, is it something you think you’d use? Or will you still favor re-typing commands in the shell instead? Make a wise choice!

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!