Back to Fish

contains - test if a word is present in a list¶

site/docs/3.2/cmds/contains.html

latest1.4 KB
Original Source

This documents an old version of fish. See the latest release.

contains - test if a word is present in a list

Synopsis

contains [OPTIONS] KEY [VALUES...]

Description

contains tests whether the set VALUES contains the string KEY. If so, contains exits with status 0; if not, it exits with status 1.

The following options are available:

  • -i or --index print the word index

Note that, like GNU tools and most of fish's builtins, contains interprets all arguments starting with a - as options to contains, until it reaches an argument that is -- (two dashes). See the examples below.

Example

If $animals is a list of animals, the following will test if it contains a cat:

if contains cat $animalsecho Your animal list is evil!end

This code will add some directories to $PATH if they aren't yet included:

for i in ~/bin /usr/local/binif not contains $i $PATHset PATH $PATH $iendend

While this will check if hasargs was run with the -q option:

function hasargsif contains -- -q $argvecho '$argv contains a -q option'endend

The -- here stops contains from treating -q to an option to itself. Instead it treats it as a normal string to check.