

For the first code snippet to run correctly, $list
would need to be put in double quotes: echo "$list" | ...
, because otherwise echo
will conflate the various lines into a single line.
The for loop approach is indeed quite readable. To make it solve the original task (which here means that it should also assign a number just smaller than $threshold to $tail, if $threshold is not itself contained in $list), one will have to do something in the spirit of what @Ephera@lemmy.ml and I describe in these comments.
Ah that’s good to know about zsh.
Sorry regarding the second code block; it does indeed work as intended, and quite elegantly.