fc (Unix)
{{lowercase}}
{{Infobox software
| name = fc
| logo =
| screenshot =
| screenshot size =
| caption =
| author =
| developer = Various open-source and commercial developers
| released =
| latest release version =
| latest release date =
| operating system = Unix, Unix-like
| platform = Cross-platform
| genre = Command
| license =
| website =
}}
{{code|fc}} is a standard program on Unix and Unix-like operating systems that lists, edits and re-executes commands previously entered to an interactive shell. Its name is an initialism for "fix command".{{Cite web |title=IEEE Std 1003.1-2024 - fc § Rationale |url=https://pubs.opengroup.org/onlinepubs/9799919799/utilities/fc.html#tag_20_44_18 |access-date=2025-04-22 |website=pubs.opengroup.org}} It is particularly helpful for editing complex, multi-line commands.
The fc command is required to be an "intrinsic" utility by POSIX,{{Cite web |title=The Open Group Base Specifications Issue 8 (IEEE Std 1003.1-2024) - fc § Application Usage|url=https://pubs.opengroup.org/onlinepubs/9799919799/utilities/fc.html#tag_20_44_16 |access-date=2025-04-22 |website=pubs.opengroup.org}} and as such is implemented as a builtin in the Bash, Zsh, and Almquist shells.
Invoked with no options, fc will open a text editor, allowing the user to modify the last-run command. Upon exiting the editor, the modified command is executed in the current shell. Various other command-line options are supported, allowing quick substitutions, repetition or modification of a specific command from the session history, or a range of commands from the history.
Examples
When invoked with the -l
flag, fc will list recent lines from the session history (the last 16 lines by default in the Bash shell):
$ fc -l
1 pwd
2 whoami
3 ls
4 ls -a
When invoked with -s PATTERN
, fc will re-run the most recent command matching PATTERN:
$ fc -s ls
ls -a
. .. .bash_logout .bashrc .profile
Though, more powerfully, -s
enables inline substitution:
$ ls floder # user typo
$ fc -s flod=fold # revise and re-run with correction
ls folder
Invoking {{code|fc}} with no arguments edits the last command executed with the user's preferred text editor. Upon exiting the editor, the modified command will execute in the current shell.{{Cite web | url=https://stavshamir.github.io/bash/fix-history-edit-the-last-bash-command/ | title=Fix history: Edit the last bash command | date=5 January 2020 }}
$ fc # change 'ls -a' to 'ls -la' in editor and exit
ls -la
total 20
drwxr-x--- 2 user user 4096 Apr 22 15:38 .
drwxr-xr-x 1 root root 4096 Apr 22 15:38 ..
-rw-r--r-- 1 user user 220 Apr 22 15:38 .bash_logout
⋮
The editor to be used can be specified with the {{code|-e}} option; otherwise it is read from the value of the FCEDIT environment variable,{{Cite web |title=IEEE Std 1003.1-2024 - fc § Environment Variables |url=https://pubs.opengroup.org/onlinepubs/9799919799/utilities/fc.html#tag_20_44_08 |access-date=2025-04-22 |website=pubs.opengroup.org}} or, in some shells, EDITOR,{{Cite web |title=Bash History Builtins (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-History-Builtins.html#index-fc |access-date=2025-04-22 |website=www.gnu.org}}{{Cite web |title=The Z Shell Manual § 15.6 Parameters Used By The Shell |url=https://zsh.sourceforge.io/Doc/Release/Parameters.html#index-FCEDIT |access-date=2025-04-22 |website=zsh.sourceforge.io}}{{Cite web | title=dash FreeBSD manual page § "Builtins" |url=https://man.freebsd.org/cgi/man.cgi?query=dash&manpath=FreeBSD+14.2-RELEASE+and+Ports#DESCRIPTION |access-date=2025-04-22 |website=man.freebsd.org}} with a fallback to vi or ed.
It is also possible to edit and re-invoke a range of commands from the history:
$ fc -l
1 pwd
2 whoami
3 ls
4 ls -a
5 ls -la
$ fc 1 2 # specify start and end history entries, separated by whitespace
pwd
/home/user
whoami
user
External links
- {{man|cu|fc|SUS|process the command history list}}