View on GitHub

🧰 Utilities.sh

for a Better BASH experience!

download .ZIPdownload .TGZ

_ includes

View Source
local ERROR=''

[ $# -lt 2 ] && ERROR=true

if [ "$ERROR" != true ]
then

  local item="$1"
  shift

  local arg=''
  for arg in "$@"
  do
    [ "$arg" = "$item" ] && return 0
  done

  return 1
fi

local ERROR_USAGE="[item], [items to search]..."
local ERROR_REASON=ARGUMENT_LENGTH
local ERROR_MINIMUM_ARGUMENTS_REQUIRED=2

!include error

Assert provided items contain given item.

Arguments

  Type Description
$@ Array Collection of items to search
$1 String Item to search for

Return Values

  Description
0 Item was found
1 Item was not found
2 Argument error

Example

if _ includes "item" "$@"
then
  echo "The arguments include 'item'"
fi
View Spec
expect { example item               } toEqual "The arguments include 'item'"
expect { example foo item           } toEqual "The arguments include 'item'"
expect { example foo bar item       } toEqual "The arguments include 'item'"
expect { example "not item"         } not toEqual "The arguments include 'item'"
expect { example foo "not item"     } not toEqual "The arguments include 'item'"
expect { example foo bar "not item" } not toEqual "The arguments include 'item'"