I have the following code
printf "%8s.%s\n" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM"
That produces the expected output
24756.22971
9910.13045
7701.30924
22972.27622
I would like to save this output with leading spaces in a variable for later uses like this
printf -v output "%8s.%s\n" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM"
However, if i try to print/echo the variable $output, leading spaces are gone:
24756.22971
9910.13045
7701.30924
22972.27622
RANDOM
divided by 32768, e.g.printf "%d.%d\n" $RANDOM $(( RANDOM * 1000 / 32768 ))
, or in a shell like Zsh that supports floating point arithmetic:printf "%.3f\n" $(( RANDOM + RANDOM / 32768.0 ))
– ilkkachu Jul 17 '21 at 19:08