How to Wield the Bash Shell Like a Pro with the Bash fc Command

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

If you’re like me, then you probably have an opinion on whether or not the bash shell is a good tool. Luckily for us, we can now answer that question with one command: “fc -l”. This simple word combination displays all of your open windows in a list. However, there are many more features to explore and master if you want to use it properly!

The “bash shell command” is a command-line tool that allows users to execute commands in the Bash shell. The “bash fc” command can be used to quickly change directories, open files, and more.

How to Wield the Bash Shell Like a Pro with the Bash fc Command

Have you ever attempted to execute a command in Bash and received an error because a word was misspelled? Maybe you forgot to run the final three commands? The Bash fc command may assist make rapid adjustments and re-execute earlier commands instead of re-writing or memorizing them.

You’ll learn how to use fc to alter and re-execute commands from your command history in this tutorial. You’ll be wielding fc like the command line warrior you aspire to be after reading this article!

Prerequisites

This page will serve as a guide. You’ll need a Linux computer or a Windows 10 PC with Windows Subsystem for Linux (WSL) activated if you want to follow along step by step. Ubuntu 20.04 LTS will be used in this lesson.

The Ultimate Guide to the Linux Windows Subsystem (Windows WSL)

The fc command in Bash

fc is a Bash built-in shell tool, not a Linux program. Bash is included in almost all major Linux distributions. The Bash fc command lets you to list, amend, or execute past commands with only a few keystrokes.

In Z Shell, the fc command is also accessible (ZSH). However, in this article’s example, fc will be used in Bash.

Have you ever ran a lengthy command that turned out to be incorrect? fc will save you from having to retype the entire command. Instead, fc allows you alter the command using your own text editor. The modified command(s) will automatically run when you save your changes!

Are you ready to give fc a try? Let’s get started!

Previous Commands List

Have you ever walked away from your computer, returned, and completely forgotten which command you used last? Maybe you can’t remember whether you’ve run many instructions in the correct order. What exactly do you do? Relax; fc will assist you in remembering. This is how.

1. On your PC, open a Bash terminal.

2. For fc to be useful, it has to operate with some form of command history. There may not be a command history since you’ve just launched a fresh terminal window. Copy each command below and execute it in your terminal to create a command history for testing. These instructions are non-intrusive, and they will not harm your machine.

# clear the screen uname -a # output a text to the terminal session echo ‘working on the command line with fc’ # Get process information using the PS command ps aux

3. Run the command below to list all commands in the history. The -l argument tells fc to list all previous commands instead of running them.

The fc command, as seen below, showed all four commands you had already provided. The numbers on the left represent the sequence in which each command was performed.

fc displays the command history.fc displays the command history.

Re-executing and editing the previous command

Consider the following scenario: you’re installing a program and you make a spelling error. As a consequence, the command is unsuccessful. Instead of retyping the whole command, why not modify it and have it run automatically? Follow these instructions to do so.

1. Run the command below in your terminal to simulate installing a new program. You’ll notice that you’ll misspell the application name caffiene on purpose.

caffiene -y sudo apt install

As a consequence, the installation attempt fails. A notice similar to the one below will appear.

Message of installation failureMessage of installation failure

2. Run the command below in the terminal to correct the previous command you ran. The command history buffer will be opened in the default text editor. Nano is the default text editor in this case.

The final command in the nano editor is shown in the screenshot below.

Nano is being used to edit the previous command. Nano is being used to edit the previous command.

If nano isn’t your favorite text editor, you may tell fc to use something else, like vi. Include the -e option before the name of the text editor. fc -e vi, for example, will display the command history in the vi text editor.

3. In the nano text editor, change the spelling of the program name from caffiene to caffeine.

4. To quit nano, hit CTRL+X after adjusting the program name.

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

Making modifications to the previous command and storing them to the history buffer Making modifications to the previous command and storing them to the history buffer

fc automatically runs the command you modified once you close the text editor.

Re-executing the edited command automaticallyRe-executing the edited command automatically

Re-executing and Editing a Previous Command

Not only can you change the last command you ran with fc, but you can also edit any of your prior commands. Follow these steps, for example, to alter the command echo ‘working on the command line with fc’.

1. Run the command below to list the preceding command in the terminal.

2. From the command history list, locate the command you wish to change and note the number to its left. The command number in this case is 2.

Finding the command number to modify in the command historyFinding the command number to modify in the command history

3. Run the command below in the terminal to modify the command with the entry number of 2. The command will open a text editor where you may modify it.

4. Once in the text editor, make any necessary changes to the command.

5. To quit nano, hit CTRL+X after altering the program.

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

In the history buffer, editing a previous command entryIn the history buffer, editing a previous command entry

The command you amended is immediately executed in the shell once you save it and quit the text editor.

Multiple Commands Editing and Execution

You’re in for a treat if you believe fc can only alter and execute one prior command at a time. Why? You may also use fc to alter several prior commands! What is the benefit of altering numerous earlier commands? You may, for example, adjust the sequence in which commands are executed.

Let’s say you wish to change the numbers 2 to 7 in the preceding command. To do so, follow these steps.

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.

2. Make any necessary changes to the orders. Consider the following scenario:

  • Delete the caffiene -y sudo apt install command.
  • Uname -a and ps aux should be run in reverse order.
  • Clear the screen.

At the same time, editing entries 2 through 7 of the command historyAt the same time, editing entries 2 through 7 of the command history

3. To quit nano after altering the instructions, hit CTRL+X.

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

After you close the text editor, the commands you altered will run automatically.

Re-executing the edited command automaticallyRe-executing the edited command automatically

Re-running a previous command without making any changes

Let’s say you merely need to run a prior command without previously changing it. There is an option for it in fc. To run a prior command from a specific item in the history, follow the instructions below.

  1. Run the command below to list the preceding command on the terminal.

2. Find the command you wish to execute again in the command history list and note the number to its left. The command number in this case is 2.

Finding the command number to run from the command historyFinding the command number to run from the command history

3. In the shell, enter the command below to perform the preceding command. The -s option tells fc to skip the editing and go straight to command number 2.

Fc ran command number 2 from the command history in the screenshot below.

Using the history buffer to execute a previous command without modifying itUsing the history buffer to execute a previous command without modifying it

Conclusion

This post aimed to stimulate your interest in fc, an important but sometimes overlooked and forgotten command. You learnt how to utilize fc to prevent wasting time by retyping previously run commands.

Is fc something you think you’d use now that you’ve learned more about it? Will you continue to prefer retyping commands in the shell? Make an informed decision!

Related Tags

  • bash shell command not found
  • bash command line arguments