Question

I'm translating this Zsh function to Fish

function random_quote() {
  QUOTE_FILES=( $PREFS_ROOT/quotes/* )
  cat $QUOTE_FILES[$RANDOM%$#QUOTE_FILES+1]
}

Here's what I've got so far:

function random_quote
  set QUOTE_FILES $PREFS_ROOT/quotes/*
  cat $QUOTE_FILES[$RANDOM%$#QUOTE_FILES+1]
end

The cat line needs fixing still. I know that RANDOM should be replaced by random, but I'm not sure how to do the rest.

Was it helpful?

Solution

How about

function random_quote
  set -l QUOTE_FILES $PREFS_ROOT/quotes/*
  set -l n (math 'scale=0;'(random)'%'(count $QUOTE_FILES)'+1')
  cat $QUOTE_FILES[$n]
end

This answer is outdated. Please follow @Niklas's correct one.

OTHER TIPS

Just found out it's really simple, random itself supports getting a random entry from a list!

just use cat (random choice $QUOTE_FILES)

it also works without a variable random choice /path/to/some/folder/*

or with a bunch of arguments random choice option1 option2 option3

you can find the documentation for random here: https://fishshell.com/docs/current/cmds/random.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top