Search code examples
arraysreadabilityfishmaintainability

Can I make a long fish array easier to read and maintain?


I am writing a simple script in fish. I need to pass in an array as follows:

set PACKAGES nginx supervisor rabbitmq-server
apt install $PACKAGES

But as the array gets longer it gets harder to read and maintain...

set PACKAGES nginx supervisor rabbitmq-server libsasl2-dev libldap2-dev libssl-dev python3-dev virtualenv

Is there another way to define an array that is easier to read? For example, vertically with comments:

set PACKAGES
    nginx
    supervisor
    rabbitmq-server

    # LDAP packages
    libsasl2-dev
    libldap2-dev
    libssl-dev

    # Python packages
    python3-dev
    virtualenv
end

Solution

    • You can escape the newline to continue the current command on the next line (and lines with comments are ignored)

    • You can use multiple set invocations

    e.g.

    set PACKAGES \
          nginx supervisor rabbitmq-server \
          # Python packages
          python3-dev virtualenv
    
     # LDAP
     set PACKAGES $PACKAGES libsasl2-dev libldap2-dev libssl-dev
    

    In current fish git, set has gained "--append"/"-a" and "--prepend"/"-p" options so you don't need to repeat the variable name (the "$PACKAGES" above).