Linux fc Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the fc (fix command) command in Linux, which allows you to edit and reexecute previous commands. The fc command is part of the Bash shell and provides a way to manipulate the command history, making it a useful tool for improving productivity and efficiency when working in the terminal. You will start by understanding the basic usage of the fc command, including how to edit and rerun previous commands, and then explore various options to customize its behavior.

The lab covers the following key steps:

  1. Understand the fc Command: Learn about the fc command and how it can be used to edit and reexecute previous commands in the Bash shell.
  2. Use fc to Edit and Reexecute Previous Commands: Explore how to use the fc command to edit and rerun previous commands, including the ability to specify a command by its number or edit the most recent command.
  3. Customize fc Command Behavior: Discover the various options available to customize the behavior of the fc command, such as listing the command history without line numbers or specifying a different editor to use for editing the commands.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") subgraph Lab Skills linux/cat -.-> lab-422675{{"`Linux fc Command with Practical Examples`"}} linux/echo -.-> lab-422675{{"`Linux fc Command with Practical Examples`"}} end

Understand the fc Command

In this step, you will learn about the fc (fix command) command in Linux, which allows you to edit and reexecute previous commands.

The fc command is part of the Bash shell and is used to manipulate the command history. It provides a way to edit and rerun previous commands, making it a useful tool for improving productivity and efficiency when working in the terminal.

To get started, let's first check the command history using the history command:

$ history
 1 ls
 2 cd project
 3 touch file.txt
 4 echo "Hello, World!" > file.txt
 5 cat file.txt

Now, let's say you want to edit the command that created the file.txt file. You can use the fc command to do this:

$ fc 4
## This will open the command in your default text editor (e.g., nano, vim)

After making the desired changes to the command, save and exit the editor. The edited command will be executed automatically.

Example output:

echo "Hello, World! Updated" > file.txt

You can also use the fc command without specifying a command number to edit the most recent command:

$ fc
## This will open the most recent command in your default text editor

The fc command also supports various options to customize its behavior, such as:

  • fc -l: List the command history without opening the editor.
  • fc -n: List the command history without line numbers.
  • fc -e editor: Specify a different editor to use for editing the commands.

Let's try listing the command history without line numbers:

$ fc -n -l
ls
cd project
touch file.txt
echo "Hello, World!" > file.txt
cat file.txt

Use fc to Edit and Reexecute Previous Commands

In this step, you will learn how to use the fc command to edit and reexecute previous commands in the Bash shell.

Let's start by checking the command history again:

$ history
 1 ls
 2 cd project
 3 touch file.txt
 4 echo "Hello, World!" > file.txt
 5 cat file.txt
 6 fc 4

As you can see, the previous step, we used fc 4 to edit the command that created the file.txt file.

Now, let's say you want to edit and reexecute the cat file.txt command. You can do this using the fc command:

$ fc 5
## This will open the "cat file.txt" command in your default text editor

Make the desired changes to the command, save, and exit the editor. The edited command will be executed automatically.

Example output:

cat file.txt
Hello, World! Updated

You can also use the fc command without specifying a command number to edit the most recent command:

$ fc
## This will open the most recent command in your default text editor

The fc command is a powerful tool for improving your productivity in the terminal. By allowing you to easily edit and reexecute previous commands, it can save you time and reduce the risk of making mistakes when typing complex or long commands.

Customize fc Command Behavior

In this final step, you will learn how to customize the behavior of the fc command to better suit your needs.

The fc command has several options that allow you to control its behavior. Let's explore a few of them:

  1. Specify a Different Editor:
    By default, the fc command uses the editor specified by the FCEDIT environment variable, or the EDITOR variable if FCEDIT is not set. You can override this by using the -e option:
$ fc -e nano
## This will open the command in the nano editor
  1. List Command History without Line Numbers:
    If you prefer to see the command history without line numbers, you can use the -n option:
$ fc -n -l
ls
cd project
touch file.txt
echo "Hello, World!" > file.txt
cat file.txt
  1. Edit a Range of Commands:
    You can also edit a range of commands by specifying the start and end command numbers:
$ fc 3 5
## This will open the commands from 3 to 5 in the editor
  1. Reexecute the Edited Command Directly:
    Instead of opening the command in the editor, you can reexecute the edited command directly using the -s option:
$ fc -s 4
## This will reexecute the command that created the file.txt file

By exploring these customization options, you can tailor the fc command to your specific needs and improve your productivity when working in the terminal.

Summary

In this lab, you learned about the fc command in Linux, which allows you to edit and reexecute previous commands. You started by understanding the basic usage of the fc command, including how to edit and rerun previous commands, as well as how to customize its behavior using various options. You then practiced using the fc command to edit and reexecute previous commands, which can be a valuable tool for improving productivity and efficiency when working in the terminal.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like