Fish shell: if with array

nabbisen - Jul 10 '20 - - Dev Community

With fish shell, there are two ways to check if some value exists in array. One is to define array and then use it in if statement, and the other is define array directly in if.

  1. Define array and then use it in if statement

    • Define array: set -l $ARRAY_NAME VALUE1 VALUE2 ....
    • Use it in if statement: if contains $TARGET $ARRAY_NAME

    For example:

    set -l myarray 'member001' 'member002'
    if contains 'member001' $myarray
        #do something
    end
    
  2. Define array directly in if statement
    Curly brackets do the work.

    For example:

    if contains 'member001' {'member001' 'member002'}
        #do something
    end
    
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .