fc command is a command line utility for listing, editing and re-executing commands previously entered into an interactive shell. The
fc command is a shell builtin meaning the command comes from the shell rather than the operating system. As such
fc can vary slightly depending on the shell being used. The
fc command is present is most shells including
fc command can be very useful for editing long commands entered into a shell and re-executing them without having to rewrite the entire command. The examples here are from the
To edit and run the last command entered into a shell run the
fc command. This will open a text editor and allow the command to be edited. When the file is saved the command will be run. This can be very useful for editing and re-executing complicated commands in a shell.
echo 'last command' last command fc ...file edited, string changed to 'hello'.. echo 'hello' hello
The text editor invoked by
fc can be specified firstly in the
FCEDIT environment variable. In the following example the
FCEDIT environment variable is set to
nano. When the
fc command is run the
nano text editor will be invoked. If
FCEDIT is not set the
EDITOR environment variable will be used. After that the
vi command will be used.
export FCEDIT=nano ls fc
To list previous commands and not execute them pass the
-l option. This will show a list of commands run in the current interactive shell.
fc -l 700 ip link set dev wlp3s0 down 701 sudo ip link set dev wlp3s0 down 702 ip a 703 dmesg ...
To start from a specific command pass the number to
fc along with the
fc -l 600 600 cat /etc/modules-load.d/bcwc-pcie.conf 601 sudo reboot 602 startx ...
To list a specific range of commands the first and last numbers may also be passed.
fc -l 701 702 701 sudo ip link set dev wlp3s0 down 702 ip a
To suppress numbers in listings the
-n can be used.
fc -ln 701 702 sudo ip link set dev wlp3s0 down ip a
To edit and re-execute a previous command first find the command to edit using
fc -l. Then pass the number to be edited and re-executed.
fc -l 841 echo 'hello' 842 echo 'world' fc 841 echo 'hello' hello
To set the text editor that is used to edit commands dynamically pass the editor to be used to the
-e option. In the following example the
pwd command will be edited in
pwd fc -e vi
On saving the file the command will be run.
zsh shell it is possible to see when commands were run by passing the
-d option to show the timestamp for each command. In the following example the
-l options is also passed to only list commands.
fc -ld 700 14:43 ip link set dev wlp3s0 down 701 14:43 sudo ip link set dev wlp3s0 down 702 14:43 ip a
A full timestamp may be shown with the
fc -lf 700 9/28/2016 14:43 ip link set dev wlp3s0 down 701 9/28/2016 14:43 sudo ip link set dev wlp3s0 down 702 9/28/2016 14:43 ip a
For a European date format the
-E option may be used.
fc -lE fc -lE 700 702 700 28.9.2016 14:43 ip link set dev wlp3s0 down 701 28.9.2016 14:43 sudo ip link set dev wlp3s0 down 702 28.9.2016 14:43 ip a
To re-execute a command without invoking an editor pass the
-e option with a dash. This will cause the command to be run immediately.
echo 'hello' hello fc -e - echo 'hello' hello
This can also accept a command number or a range of numbers from the history.
fc -l 842 echo 'hello world' 843 fc -e - 844 echo 'hello' fc -e - 842 echo 'hello world' hello
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Using HashiCorp Vault with LDAP
How to use HashiCorp Vault to setup an LDAP backed secret store with read-only access for users in groups and read-write access for specific users
Linux and Unix xargs command tutorial with examples
Tutorial on using xargs, a UNIX and Linux command for building and executing command lines from standard input. Examples of cutting by character, byte position, cutting based on delimiter and how to modify the output delimiter.
Copy a file in Go
How to copy a file in Go. The ioutil package does not offer a shorthand way of copying a file. Instead the os package should be used.