I use sed with <<< and read to assign all words in a string to variables. What I do is:read -a A0 <<< $(sed '2q;d' /proc/stat)Hence, sed reads the second line of the file file and immediately quits. The line sed has read in is fed to <<< which does expands the input it receives from sed and read -a assign the resulting values of <<< to elements of an array. Is the usage of <<< and sed in this way a good idea or are there obvious reasons that speak against this and most of all, is there a faster way to do this?
Using sed with herestring (<<
bash;shell script;sed;read
You can use array assignment directly:A0=($(sed '2q;d' /proc/stat))Beware that this performs globbing: if the output of the command contains shell wildcards, then the words containing wildcards are replaced by the list of matching files if there are any. If the output of the command might contain one of the characters \[?*, temporarily turn off globbing:set -fA0=($(sed '2q;d' /proc/stat))set +fThis can be tiny faster than using read:$ time for i in {1..1000}; do read -a A0 <<< $(sed '2q;d' /proc/stat); donereal    0m2.829suser    0m0.220ssys     0m0.480s$ time for i in {1..1000}; do A0=($(sed '2q;d' /proc/stat)); donereal    0m2.388suser    0m0.128ssys     0m0.276sWith bash 4.0 and above, you can use mapfile:mapfile -t < <(sed '2q;d' /proc/stat)But mapfile seems to be slowest:$ time for i in {1..1000}; do mapfile -t < <(sed '2q;d' /proc/stat); donereal    0m3.990suser    0m0.104ssys     0m0.444s