LinuxCommandLibrary

histexpand

Execute commands from history using event designators

TLDR

Run the previous command as root (!! is replaced by the previous command)

$ sudo !!
copy


Run a command with the last argument of the previous command
$ [command] !$
copy


Run a command with the first argument of the previous command
$ [command] !^
copy


Run the Nth command of the history
$ ![n]
copy


Run the command n lines back in the history
$ !-[n]
copy


Run the most recent command containing string
$ !?[string]?
copy


Run the previous command, replacing string1 with string2
$ ^[string1]^[string2]^
copy


Perform a history expansion, but print the command that would be run instead of actually running it
$ [!-n]:p
copy

SYNOPSIS

set -o histexpand
set +o histexpand
shopt -s histexpand
shopt -u histexpand

PARAMETERS

set -o histexpand
    Enables the histexpand shell option. When active, history expansion is performed, allowing ! syntax to recall and modify previous commands.

set +o histexpand
    Disables the histexpand shell option. The ! character will be treated as a literal character, preventing history expansions.

shopt -s histexpand
    Another way to enable the histexpand shell option, specific to Bash shell options. This provides the same functionality as set -o histexpand.

shopt -u histexpand
    Another way to disable the histexpand shell option, specific to Bash shell options. This provides the same functionality as set +o histexpand.

DESCRIPTION

histexpand is a fundamental shell option in Bash that, when enabled, allows for history expansion. This powerful feature enables users to efficiently re-execute, modify, or insert specific parts of previously executed commands into the current command line. It primarily utilizes the ! character to perform these expansions. For instance, !! instantly re-executes the last command, !$ refers to the last argument of the preceding command, and !grep executes the most recent command that started with 'grep'. Disabling histexpand (or the ! option, which is equivalent for set -o) prevents these intelligent expansions, treating ! as a literal character in most contexts, except when followed by a space, newline, '=', or '('. In interactive Bash sessions, histexpand is typically enabled by default, significantly streamlining command-line productivity. Users can query its status using set -o or shopt.

CAVEATS

History expansion can sometimes lead to unintended command execution if not used carefully, especially when dealing with commands containing !. Users should be aware of the exact expansion before pressing Enter. If histexpand is disabled, the ! character will lose its special meaning for history recall. The shell variable HISTCHARS can influence which characters trigger history expansion.

<B>COMMON HISTORY EXPANSIONS</B>

!!
Refers to the last command.

!$
Refers to the last argument of the previous command.

!*
Refers to all arguments of the previous command.

!string
Refers to the most recent command beginning with 'string'.

!?string?
Refers to the most recent command containing 'string'.

!N
Refers to command number N in the history list.

!-N
Refers to the Nth previous command.

^string1^string2^
A quick substitution to replace 'string1' with 'string2' in the previous command. This form does not require histexpand to be enabled if shopt -s cmdhist is off, but it's part of the general history manipulation features.

HISTORY

History expansion has been a feature of Unix shells, particularly csh (C Shell), since the 1970s. Bash later adopted and enhanced this functionality, making it a standard and powerful interactive feature. The histexpand option specifically provides granular control over whether this behavior is active. It's often enabled by default in interactive Bash sessions for convenience and productivity.

SEE ALSO

bash(1), history(1), set(1), shopt(1), fc(1)

Copied to clipboard