On Mon, Oct 26, 2026 at 19:21:48 +0100, Franco Martelli wrote:
> As usual thanks Greg for the clarifications. I'm still playing with the RNG
> code of the script that shuffle a playlist, since it depends on a list of
> filenames in a given directory, I'd like now that the indexed array
> "playlist" be populated by a sequence of numbers instead of filenames, this
> works:
> 
> playlist=({001..136})

I don't understand the need for this array, but hold that thought.

> but the whole sequence is expanded on a single line. I'd like that each
> number be separated by newline \n.

printf '%d\n' "${playlist[@]}"

I still don't know why you want that output, though.

Now, back to the original thought.  If you've got a playlist array
that contains filenames, then that array is *already* numerically
indexed.

playlist=(clip1 clip2 clip3 clip4)
for i in "${!playlist[@]}"; do
    printf 'Index %d is "%s"\n' "$i" "${playlist[i]}"
done

Running this code gives us:

Index 0 is "clip1"
Index 1 is "clip2"
Index 2 is "clip3"
Index 3 is "clip4"

So, from what I can see, you only want to make two changes:

  1) Count starting from 1 instead of 0.
  2) Zero-pad each index to 3 digits.

printf plus the shell's builtin arithmetic allows us to do this easily:

playlist=(clip1 clip2 clip3 clip4)
for i in "${!playlist[@]}"; do
    printf 'Index %03d is "%s"\n' "$((i+1))" "${playlist[i]}"
done

That gives us:

Index 001 is "clip1"
Index 002 is "clip2"
Index 003 is "clip3"
Index 004 is "clip4"

Is that all you needed?

Reply via email to