LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

caret

Shell history substitution and regex anchor character

TLDR

Quick substitution on last command
$ ^[old]^[new]
copy
Repeat last command replacing text
$ ^typo^fixed
copy
Start of line anchor (regex)
$ grep "^start" [file]
copy
Negation in character class (regex)
$ grep "[^0-9]" [file]
copy

SYNOPSIS

^old^new[^]

DESCRIPTION

^ in shell has multiple meanings:History substitution: ^old^new is a shortcut for !!:s/old/new/, replacing text in the previous command. Quick fix for typos without retyping.Regular expression anchor: ^ matches the start of a line. ^hello matches lines starting with "hello".Character class negation: [^abc] matches any character except a, b, or c.Exponentiation: In $((...)) and some languages, ^ may be XOR or exponent (use ******** in bash for exponent).

PARAMETERS

^old^new

Replace first occurrence of old with new in the previous command and execute it.
^old^new^
Same substitution; the trailing ^ is optional unless appending additional text.
!!:s/old/new/
Equivalent long-form history substitution syntax.
!!:gs/old/new/
Replace all occurrences of old with new in the previous command.

CAVEATS

^old^new only replaces the first occurrence. For global replacement, use !!:gs/old/new/.In some shells/contexts, ^ may need escaping or behave differently.The caret substitution must be at the start of the command line.

SEE ALSO

bash(1), history(1), grep(1), sed(1)

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid