DEV Community

loading...

Fish shell: if with array

Heddi Nabbisen
An ICT designer/developer and a security monk with "a cool brain and a warm heart". OpenBSD/Rust etc. Interested in social issues and relationships. Founder of Scqr Inc.
・1 min read

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
    

Discussion (0)