It's a one-liner, using bash process substitution.
$ read lines words bytes filename < <(wc /path/to/file)
Output is also a one-liner, and you can leverage bash arrays if you like. Here's a real example:
$ read -a arr < <(wc /etc/passwd);
$ declare -p arr
declare -a arr=([0]="96" [1]="265" [2]="5925" [3]="/etc/passwd")
$ unset arr[3]
$ printf 'lines: %d\nwords: %d\nbytes: %d\n' "${arr[@]}"
lines: 96
words: 265
bytes: 5925
Similar results in POSIX shell might be achieved using a temporary file:
$ tmp=$(mktemp /tmp/foo.XXXX)
$ wc /etc/passwd > $tmp
$ read lines words bytes filename < $tmp
$ rm $tmp
$ printf 'lines: %d\nwords: %d\nbytes: %d\n' "$lines" "$words" "$bytes"
lines: 96
words: 265
bytes: 5925
Or you can fetch the data as a here-doc without the temporary file:
$ read lines words bytes filename <<EOT
> $(wc /etc/passwd)
> EOT
(Obviously, you'd strip the interactive prompts if you were to script this.)
Note that printf
recommended over echo
for output, as it is consistent across different operating systems and shells. This excellent post explains some considerations.