Under Construction

Re-execute Commands "fc -s"

In the event that previous commands are to be executed again without changes, or only with small changes, the fc command offers an even faster option. Instead of first loading the command into an editor and then executing it, it can also be re-executed again directly. Minor changes (replacing a character string with another character string) are also possible easily. To do this, fc can be called with the “-s” (substitute) option. One or more replacements can be specified as an argument in the form of “old=new“. where old is the string to be replaced and new is the replacement string.

Let’s assume the following ls command was entered:

[user01@aixe01 ~]$ ls -ld /usr/bin
drwxr-xr-x    4 bin      bin           53248 Apr 21 08:41 /usr/bin/
[user01@aixe01 ~]$

Immediately afterwards, the same information on the /usr/adm directory should be listed. All you have to do is to replace the “bin” character string with the “adm” character string in the last command, i.e. “bin=adm“:

[user01@aixe01 ~]$ fc -s bin=adm
ls -ld /usr/adm
lrwxrwxrwx    1 root     adm               8 Mar 24 14:41 /usr/adm@ -> /var/adm/
[user01@aixe01 ~]$

If no command is specified, “fc -s” refers to the last command. However, as usual, you can also enter a command using the number of the command or in the form of a character string. As an example, let’s assume the following history:

[user01@aixe01 ~]$ history
    1  myscript stop
    2  pwd
    3  hostname
    4  ls -ld /usr/bin
    5  ls -ld /usr/adm
[user01@aixe01 ~]$

The command with the number 1 is to be executed again, but the argument “start” is to be used instead of the argument “stop“. The command can either be selected via the number (here 1) or the character string “myscript” or even shorter “my” (or just “m“). The argument “stop” can be replaced with “start” using e.g. the substitution “op=art“:

[user01@aixe01 ~]$ fc -s op=art my
myscript start
[user01@aixe01 ~]$

In order not having to type always “fc –s” (that’s 5 characters), many bash users define the following alias:

alias r=“fc –s“

Note: Here “r” is meant to stand for re-execute.

This allows, for example, the last command to be executed again simply by using “r” without any arguments. This is even one character shorter than the version “!!” with history expansion.

The “fc –s” command also supports multiple replacements, as the following example shows: The character string “hello world” is to be printed using echo. Due to two typos, the following came out:

[user01@aixe01 ~]$ echo hallo wurld
hallo wurld
[user01@aixe01 ~]$

In the first argument, “hallo” would have to be replaced with “hello” (“a=e“) and in the second argument, “wurld” would have to be replaced with “world” (“u=o“). This can easily be achieved with the following command:

[user01@aixe01 ~]$ r a=e u=o
echo hello world
hello world
[user01@aixe01 ~]$

Note: All occurrences of the specified character strings are always replaced!

The syntax of “fc -s” is definitely simpler than the syntax of the history expansions presented earlier. However, the options are more limited.