bash bang - macOS


!! – Repeat Last Command

Overview

The !! command in macOS provides a quick and convenient way to repeat the last executed command in the Terminal. It is particularly useful for re-entering long or complex commands without having to type them manually.

Syntax

!!

Options/Flags

None

Examples

  • Simple use: To repeat the ls command, type !!.
  • Complex use: To re-enter a command with arguments, use !! followed by the arguments. For example, to repeat the find . -name "*.txt" command, type !! *.txt.

Common Issues

  • Command not found: If !! returns “command not found,” it means the last executed command is no longer available in the history.
  • Incorrect command: If the repeated command does not execute correctly, it may have been modified or contained errors.

Integration

  • Script automation: !! can be incorporated into scripts to automate repetitive tasks. For example, a script could use !! to re-execute a command X times, each time with different input parameters.
  • Pipeline chaining: !! can be combined with pipes (|) to create complex command chains. For example, to pipe the output of the ls command to the grep command, type ls | grep foo.

Related Commands

  • ^: Repeats the last argument of the previous command.
  • !!n: Repeats the nth previous command.
  • history: Displays the command history.