“Bash If Element in Array” Code-Antworten

Bash If Element in Array

# Basic syntax:
if [[ "${array[*]}" =~ "${value}" ]]; then
    echo "$value is in array"
fi
# Note, relative to e.g. Python, this syntax feels a little backwards. It
#	checks if the value variable is in the array, which reads right to left.
# Note, if you want to check whether the element is not in the array, use:
if [[ ! "${array[*]}" =~ "${value}" ]]; then
    echo "$value is not in array"
fi
Charles-Alexandre Roy

Überprüfen Sie, ob ein Bash -Array einen Wert enthält

if [[ " ${array[*]} " =~ " ${value} " ]]; then
    # whatever you want to do when array contains value
fi

if [[ ! " ${array[*]} " =~ " ${value} " ]]; then
    # whatever you want to do when array doesn't contain value
fi
Hutch Polecat

Ähnliche Antworten wie “Bash If Element in Array”

Fragen ähnlich wie “Bash If Element in Array”

Weitere verwandte Antworten zu “Bash If Element in Array” auf Shell/Bash

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen