Your original command can be simplified to
shuf -e A B C | tr "\n" " " && echo ""
or
shuffled=( $(shuf -e A B C) ) ; echo ${shuffled[*]}
Which I think is a little less hacky and is also faster from my rudimentary tests.
If you have a file at ~/test which contains
A B C
D E F
You can shuffle and echo each line with the following command
while IFS= read -r line; do shuffled=( $(shuf -e $line) ) ; echo ${shuffled[*]} ; done < ~/test
or in script form:
#!/bin/bash
while IFS= read -r line
do shuffled=( $(shuf -e $line) )
echo ${shuffled[*]}
done < ~/test
Where you might want to replace ~/test with $1 to pass arguments to the script.
result:
B C A
G E F
How this works:
shuf -e splits on spaces as well as newlines.. but only because it will treat A B C as three arguments.
so
shuf -e A B C
will shuffle A B and C
but shuf -e "A B C"
will not shuffle A B and C
We can use this to read each line into an array and then print it out again with echo.
while IFS= read -r line;
Reads in each line into $line when it is passed with < to this loop.
do shuffled=( $(shuf -e $line) )
Makes an array out of each line in the $shuffled variable, by literally expanding shuf -e $line to shuf -e A B C.
echo ${shuffled[*]}
echos our array, by default printing each element with spaces in between
< ~/test
feeds lines from ~/test into our loop.
cat,tr " " "\n" < <file> | shuf | tr "\n" " "is the same. – Quora Feans Sep 06 '19 at 00:04