Search code examples
bashwc

Wc and reading from a file


I want to check the number of lines of a certain file without using a temporary file using wc. I tried using something like this: var=${wc -l a.txt} or wc -l a.txt | read line but both didn't work.


Solution

  • The way to store the output of a command is var=$(command) instead of the var=${command} you used.

    Hence, this will do:

    var=$(wc -l a.txt)    # returns XX a.txt
    

    To store just the number, do

    var=$(wc -l < a.txt)  # returns XX
                ^