bashshellwc

results of wc as variables


I would like to use the lines coming from 'wc' as variables. For example:

echo 'foo bar' > file.txt
echo 'blah blah blah' >> file.txt
wc file.txt

2  5 23 file.txt

I would like to have something like $lines, $words and $characters associated to the values 2, 5, and 23. How can I do that in bash?


Solution

  • There are other solutions but a simple one which I usually use is to put the output of wc in a temporary file, and then read from there:

    wc file.txt > xxx
    read lines words characters filename < xxx 
    echo "lines=$lines words=$words characters=$characters filename=$filename"
    lines=2 words=5 characters=23 filename=file.txt
    

    The advantage of this method is that you do not need to create several awk processes, one for each variable. The disadvantage is that you need a temporary file, which you should delete afterwards.

    Be careful: this does not work:

    wc file.txt | read lines words characters filename
    

    The problem is that piping to read creates another process, and the variables are updated there, so they are not accessible in the calling shell.

    Edit: adding solution by arnaud576875:

    read lines words chars filename <<< $(wc x)
    

    Works without writing to a file (and do not have pipe problem). It is bash specific.

    From the bash manual:

    Here Strings
    
       A variant of here documents, the format is:
    
              <<<word
    
       The word is expanded and supplied to the command on its standard input.
    

    The key is the "word is expanded" bit.