Learn to use fc commands in Linux

Fc (short for Fix Commands) is a shell built-in command for listing, editing, and executing recently entered commands in an interactive shell. You can use your favorite editor to edit recent commands and execute them again without having to retype them all. In addition to the long and complicated commands that can be avoided, it is also useful for correcting misspellings. Because it is a shell built-in command, most shells include it, such as Bash, Zsh, Ksh, and so on. In this short article, we come to learn to use the fc command in Linux.

Fc command tutorials and examples

List recently executed commands

Execute the fc -l command with no arguments, it will list the last 16 commands.

$fc -l

507fish

508fc -l

509sudo netctl restart wlp9s0sktab

510ls -l

511pwd

512uname -r

513uname -a

514touch ostechnix.txt

515vi ostechnix.txt

516echo"Welcome to OSTechNix"

517sudo apcman -Syu

518sudo pacman -Syu

519more ostechnix.txt

520wc -lostechnix.txt

521cat ostechnix.txt

522clear

The -r option is used to reverse the output.

$fc -lr

The -n option is used to hide the line number.

$fc -ln

Nano ~/.profile

Source ~/.profile

Source ~/.profile

Fc -ln

Fc -l

Sudo netctl restart wlp9s0sktab

Ls -l

Pwd

Uname -r

Uname -a

Echo"Welcome to OSTechNix"

Sudo apcman -Syu

Cat ostechnix.txt

Wc -lostechnix.txt

More ostechnix.txt

Clear

This line number is no longer displayed.

If you want to start with a command, just add the line number after the -l option. For example, to display line number 520 to the nearest command, you could do this:

$fc -l520

520ls -l

521pwd

522uname -r

523uname -a

524echo"Welcome to OSTechNix"

525sudo apcman -Syu

526cat ostechnix.txt

527wc -lostechnix.txt

528more ostechnix.txt

529clear

530fc ​​-ln

531fc -l

To list a range of commands, use the first and last line numbers as parameters of fc -l, such as 520 to 525:

$fc -l520525

520ls -l

521pwd

522uname -r

523uname -a

524echo"Welcome to OSTechNix"

525sudo apcman -Syu

In addition to using line numbers, we can also use characters. For example, to list all the commands between the most recent pwd to the most recent command, just use the start letter as follows:

$fc -lp

521pwd

522uname -r

523uname -a

524echo"Welcome to OSTechNix"

525sudo apcman -Syu

526cat ostechnix.txt

527wc -lostechnix.txt

528more ostechnix.txt

529clear

530fc ​​-ln

531fc -l

532fc -l520

533fc -l520525

534fc -l520

535fc -l522

536fc -ll

To list all commands between pwd and more, you can use the starting letters like this:

$fc -lpm

Or, use the first letter of the start command and the line number of the end command:

$fc -lp528

Or use the line number:

$fc -l521528

All three commands show the same result.

Edit and execute the previous command

We often type wrong commands. At this point, you can use the default editor to correct spelling mistakes and execute them without having to repeat the command again.

Edit and execute the previous command:

$fc

This will load the previous command in the default editor.

As you can see, my previous command was fc -l. You can modify it as you like. It will be executed automatically when you save and exit the editor. This is useful when the command or parameter is long and complicated. It should be noted that it may also be devastating. For example, if your last command is dangerous rm -fr You may lose your important data when it is executed automatically. So, treat each command carefully.

Change the default editor

Another useful option is -e , which can be used to select a different editor for the fc command. For example, if we want to use nano to edit the previous command:

$fc -enano

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

If you find it too cumbersome to use the -e option, you can modify your default editor by setting the environment variable FCEDIT to the name of the editor you want fc to use.

For example, to set nano as the default editor, edit your ~/.profile or other initialization file: (LCTT: if ~/.profile does not exist, create it yourself; if you are using bash, edit ~/.bash_profile )

$vi ~/.profile

Add the following line:

FCEDIT=nano

# LCTT: If you use fc in a subshell, it is best to export FCEDIT here.

You can also use the full path of the editor:

FCEDIT=/usr/local/bin/emacs

Enter: wq Save and exit. To make changes effective immediately, run the following command:

$source ~/.profile

Now enter fc to edit the previous command using the nano editor.

Do not edit and execute the previous command directly

We now know that the fc command will load the previous command into the editor without any parameters. But sometimes you may not want to edit, just want to execute the previous command again. This is very simple, with a hyphen (-) at the end:

$echo"Welcome to OSTechNix"

Welcome toOSTechNix

$fc -e -

Echo"Welcome to OSTechNix"

Welcome toOSTechNix

As you can see, fc has the -e option, but it doesn't edit the previous command (example echo "Welcome to OSTechNix").

Note that some options are only valid for the specified shell. For example, the following options can be used in zsh but not in Bash or Ksh.

Display command execution time

To know when the command was executed, use the -d option:

Fc -ld

118:41exit

218:41clear

318:42fc -l

418:42sudo netctl restart wlp9s0sktab

518:42ls -l

618:42pwd

718:42uname -r

818:43uname -a

918:43cat ostechnix.txt

1018:43echo"Welcome to OSTechNix"

1118:43more ostechnix.txt

1218:43wc -lostechnix.txt

1318: 43cat ostechnix.txt

1418:43clear

1518:43fc -l

This way you can see the specific execution time of the most recent command.

With the -f option, a complete time stamp can be displayed for each command.

Fc -lf

14/5/201818:41exit

24/5/201818:41clear

34/5/201818:42fc -l

44/5/201818:42sudo netctl restart wlp9s0sktab

54/5/201818:42ls -l

64/5/201818:42pwd

74/5/201818:42uname -r

84/5/201818:43uname -a

94/5/201818:43cat ostechnix.txt

104/5/201818:43echo"Welcome to OSTechNix"

114/5/201818:43more ostechnix.txt

124/5/201818:43wc -lostechnix.txt

134/5/201818:43cat ostechnix.txt

144/5/201818:43clear

154/5/201818:43fc -l

164/5/201818:43fc -ld

Of course, European villagers can also use the -E option to display the European time format.

Fc -lE

25.4.201818:41clear

35.4.201818:42fc -l

45.4.201818:42sudo netctl restart wlp9s0sktab

55.4.201818:42ls -l

65.4.201818:42pwd

75.4.201818:42uname -r

85.4.201818:43uname -a

95.4.201818: 43cat ostechnix.txt

105.4.201818:43echo"Welcome to OSTechNix"

115.4.201818:43more ostechnix.txt

125.4.201818:43wc -lostechnix.txt

135.4.201818:43cat ostechnix.txt

145.4.201818:43clear

155.4.201818:43fc -l

165.4.201818:43fc -ld

175.4.201818:49fc -lf

Fc usage summary

When no parameters are present, fc loads the previous command into the default editor.

When you take a number as a parameter, fc loads the numerically specified command into the default editor.

When you take a character as a parameter, fc loads the most recent command that starts with the specified character into the default editor.

When there are two parameters, they specify the beginning and end of the range of commands that need to be listed.

For more details, please refer to the man manual.

$man fc

Well, today is this. Hope this article will help you.

Intel I3 Laptop

What`s your impression for Intel I3 Laptop? You can take i3 Laptop Deals as the entry level of Gaming Laptop . There are different parameters standards,like 14 Inch Laptop I3 11th Generation, 14inch 256GB Intel I5 11th Generation Laptop, 14.1 inch Intel i7 11th Generation Laptop, 15.6 inch I3 10th Generation Laptop,15 inch Intel I5 10th Generation Laptop, 15inch Intel i7 11th Generation Laptop, etc. That`s some of Top 10 Gaming Laptops. To operating OS, more than 80% clients choose windows 10, home or pro option; nowadays windows 11 is new arrival, you can choose as your demand. Rich slots meet your different potential application scenarios, home, office, public places, academic situations, etc.

Dual storage channels, support HDD and SSD. Your customers can update in future according potential demands.

Of course, other type, like yoga notebook, 2 In 1 Laptop , Android Tablet, Custom All In One PC , 14 inch Student Laptop, i7 16gb ram 4gb graphics laptop, etc. available here also. Just call us and get right details quickly.

Intel I3 Laptop,Laptop I3 11th Generation,i3 10th Generation Laptop,i3 Laptop Deals,Laptop Intel Core I3 Gen 11

Henan Shuyi Electronics Co., Ltd. , https://www.shuyilaptop.com

Posted on