STRING-MATCH(1)
π string-match(1) β fish-shell bestie manual π
okay so bestie, welcome to string-match(1) from the fish-shell manual. This is the page where string match matches substrings, and I'm gonna walk you through every flag, every example, and every little caveat like we're texting at 2am. π«Ά
πΈ NAME πΈ
string-match β match substrings. That's the whole vibe: string match does the matching. β¨
π¦ SYNOPSIS π¦
string match [-a | --all] [-e | --entire] [-i | --ignore-case]
[-g | --groups-only] [-r | --regex] [-n | --index]
[-q | --quiet] [-v | --invert] [(-m | --max-matches) MAX]
PATTERN [STRING ...]
π DESCRIPTION π
okay so string match tests each STRING against PATTERN and prints matching substrings. Only the first match for each STRING is reported unless you give -a or --all, and then it reports all matches. periodt. π
Here's the flag rundown, bestie:
-a,--allβ report all matches for eachSTRING, not just the first one. π₯-e,--entireβ each matching string is printed including any prefix or suffix not matched by the pattern. It's equivalent togrepwithout the-oflag. You can, obviously, achieve the same result by prepending and appending*or.*depending on whether or not you have specified the--regexflag. The--entireflag is a way to avoid having to complicate the pattern in that fashion and make the intent of thestring matchclearer. Without--entireand--regex, aPATTERNwill need to match the entireSTRINGbefore it will be reported. π―-i,--ignore-caseβ matching can be made case-insensitive with--ignore-caseor-i. lowercase girlie rights! π-g,--groups-onlyβ only the capturing groups will be reported, meaning the full match will be skipped. This is incompatible with--entireand--invert, and requires--regex. It is useful as a simple cutting tool instead ofstring replace, so you can choose "this part" of a string. βοΈ-r,--regexβPATTERNis interpreted as a Perl-compatible regular expression, which does not have to match the entireSTRING. For a regular expression containing capturing groups, multiple items will be reported for each match, one for the entire match and one for each capturing group. With this, only the matching part of theSTRINGwill be reported, unless--entireis given. π§-n,--indexβ each match is reported as a 1-based start position and a length. By default,PATTERNis interpreted as a glob pattern matched against each entireSTRINGargument. A glob pattern is only considered a valid match if it matches the entireSTRING. π-q,--quietβ this one is in the synopsis and it shows up in the examples as-rq; the page doesn't give it a separate paragraph of extra lore, so I'm not inventing any. π-v,--invertβ if--invertor-vis used, the selected lines will be only those which do not match the given glob pattern or regular expression. π-m MAX,--max-matches MAXβ if--max-matches MAXor-m MAXis used,stringwill stop checking for matches afterMAXlines of input have matched. This can be used as an "early exit" optimization when processing long inputs but expecting a limited and fixed number of outputs that might be found considerably before the input stream has been exhausted. If combined with--invertor-v, considers only inverted matches. π
When matching via regular expressions, string match automatically sets variables for all named capturing groups ((?<name>expression)). It will create a variable with the name of the group, in the default scope, for each named capturing group, and set it to the value of the capturing group in the first matched argument. If a named capture group matched an empty string, the variable will be set to the empty string (like set var ""). If it did not match, the variable will be set to nothing (like set var). π±
When --regex is used with --all, this behavior changes. Each named variable will contain a list of matches, with the first match contained in the first element, the second match in the second, and so on. If the group was empty or did not match, the corresponding element will be an empty string. π§Ί
Exit status:0if at least one match was found, or1otherwise. π
π EXAMPLES π
πΈ Match Glob Examples πΈ
>_ string match 'a' a
a
>_ string match 'a*b' axxb
axxb
>_ string match -i 'a*B' Axxb
Axxb
>_ string match -- '-*' -h foo --version bar
# To match things that look like options, we need a `--`
# to tell string its options end there.
-h
--version
>_ echo 'ok?' | string match '*?'
ok?
# Note that only the second STRING will match here.
>_ string match 'foo' 'foo1' 'foo' 'foo2'
foo
>_ string match -e 'foo' 'foo1' 'foo' 'foo2'
foo1
foo
foo2
>_ string match 'foo*' 'foo1' 'foo' 'foo2'
foo1
foo2
π¦ Match Regex Examples π¦
>_ string match -r 'cat|dog|fish' 'nice dog'
dog
>_ string match -r -v "c.*[12]" {cat,dog}(seq 1 4)
dog1
dog2
cat3
dog3
cat4
dog4
>_ string match -r -- '-.*' -h foo --version bar
# To match things that look like options, we need a `--`
# to tell string its options end there.
-h
--version
>_ string match -r '(\d\d?):(\d\d):(\d\d)' 2:34:56
2:34:56
2
34
56
>_ string match -r '^(\w{2,4})\1$' papa mud murmur
papa
pa
murmur
mur
>_ string match -r -a -n at ratatat
2 2
4 2
6 2
>_ string match -r -i '0x[0-9a-f]{1,8}' 'int magic = 0xBadC0de;'
0xBadC0de
>_ echo $version
3.1.2-1575-ga2ff32d90
>_ string match -rq '(?<major>\d+).(?<minor>\d+).(?<revision>\d+)' -- $version
>_ echo "You are using fish $major!"
You are using fish 3!
>_ string match -raq ' *(?<sentence>[^.!?]+)(?<punctuation>[.!?])?' "hello, friend. goodbye"
>_ printf "%s\n" -- $sentence
hello, friend
goodbye
>_ printf "%s\n" -- $punctuation
.
>_ string match -rq '(?<word>hello)' 'hi'
>_ count $word
0
π Copyright π
Copyright belongs to the fish-shell developers. π
π This manual page is from fish-shell, version 4.9, dated Sep 08, 2026 β and that's the cute sign-off, bestie. mwah! π
π¦ where this page comes from
- arch rolling: fish 4.9.3-1 Β· GPL-2.0-only AND BSD-3-Clause AND ISC AND MIT AND PSF-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms