All Posts programming fc: open the most recent command for editing and then run it

fc: open the most recent command for editing and then run it

ยท 615 words ยท 3 minute read

What is fc ? ๐Ÿ”—

fc is a tool to process the command history list.

The fc utility shall list, or shall edit and re-execute, commands previously entered to an interactive sh.

The command history list shall reference commands by number. The first number in the list is selected arbitrarily. The relationship of a number to its command shall not change except when the user logs in and no other process is accessing the list, at which time the system may reset the numbering to start the oldest retained command at another number (usually 1). When the number reaches an implementation-defined upper limit, which shall be no smaller than the value in HISTSIZE or 32767 (whichever is greater), the shell may wrap the numbers, starting the next command with a lower number (usually 1).

However, despite this optional wrapping of numbers, fc shall maintain the time-ordering sequence of the commands.

For example, if four commands in sequence are given the numbers 32766, 32767, 1 (wrapped), and 2 as they are executed, command 32767 is considered the command previous to 1, even though its number is higher.

When commands are edited (when the -l option is not specified), the resulting lines shall be entered at the end of the history list and then re-executed by sh. The fc command that caused the editing shall not be entered into the history list. If the editor returns a non-zero exit status, this shall suppress the entry into the history list and the command re-execution. Any command line variable assignments or redirection operators used with fc shall affect both the fc command itself as well as the command that results; for example:

fc -s -- -1 2>/dev/null

reinvokes the previous command, suppressing standard error for both fc and the previous command.

Open the most recent command for editing and then run it.

options & flags ๐Ÿ”—

The fc utility shall conform to the Base Definitions volume of POSIX.1โ€2017, Section 12.2, Utility Syntax Guidelines.

The following options shall be supported:

-e : editor Use the editor named by editor to edit the commands. The editor string is a utility name, subject to search via the PATH variable (see the Base Definitions volume of POSIX.1โ€2017, Chapter 8, Environment Variables). The value in the FCEDIT variable shall be used as a default when -e is not specified. If FCEDIT is null or unset, ed shall be used as the editor.

-l : (The letter ell.) List the commands rather than invoking an editor on them. The commands shall be written in the sequence indicated by the first and last operands, as affected by -r, with each command preceded by the command number.

-n : Suppress command numbers when listing with -l.

-r : Reverse the order of the commands listed (with -l) or edited (with neither -l nor -s).

-s : Re-execute the command without invoking an editor.

Usage ๐Ÿ”—

Open the last command in the default system editor and run it after editing:

fc

Specify an editor to open with:

fc -e 'emacs'

List recent commands from history:

fc -l

List recent commands in reverse order:

fc -l -r

Edit and run a command from history:

fc number

Edit commands in a given interval and run them:

fc '416' '420'

Display help:

fc --help

For more detailed information, check out the man page of fc: https://manned.org/fc .

I hope you enjoyed reading this post as much as I enjoyed writing it. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , and GitHub .