Search code examples
arrayssortingshufflefish

How can I sort an array randomly in Fish Shell?


I have an array of strings, I want to shuffle the order of the strings.

I found this question but it deals with bash and I'm using fish shell. Is there a way to achieve this?


Solution

  • Using shuf as with the Bash version, it's a lot cleaner in Fish:

    Using $PATH as an example, since it's a preexisting array of strings:

    shuf -e (string collect $PATH)
    # or
    set random_path (shuf -e (string collect $PATH))
    

    The variable assignment assumes that there are no newlines in the existing array elements.