Linux command
caret 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Quick substitution on last command
^[old]^[new]
Repeat last command replacing text
^typo^fixed
Start of line anchor (regex)
grep "^start" [file]
Negation in character class (regex)
grep "[^0-9]" [file]
说明
^ 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).
参数
- ^_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.
FAQ
What is the caret command used for?
^ 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).
How do I run a basic caret example?
Run `^[old]^[new]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does ^_old_^_new_ do in caret?
Replace first occurrence of _old_ with _new_ in the previous command and execute it.