Command output redirection in script not working

I need to count the number of lines in a .txt file and put it in a variable.

I am using the following code

#!/bin/bash

count = $(wc -l "some file.txt" | awk '{print$1}')
echo $count

It is giving the following error.

line3: count: command not found

What am I doing wrong here? :confused:

Spacing

#!/bin/bash  
count=$(wc -l "some file.txt" | cut -d\  -f1) 
echo $count
1 Like
count=$(wc - l < file)
1 Like
awk 'END { print NR }' file
1 Like

As Skrynesaver said it is spacing:

count=$(wc -l < file)

:smiley:

--
There are several methods of determining the number of lines in a file, another one for example:

sed -n $= file

But wc -l is by far the most efficient of the standard Unix utils ( see Alternative for wc -l: Comparison )

--
@Skrynesaver, this does not work universally, since some wc's right-align the number:

$ wc -l infile |  cut -d\  -f1
$ wc -l infile                  
       4 infile
$ wc -l < infile
       4
$ wc -l infile | awk '{print $1}'
4
1 Like
count=`cat some_file|wc -l`
echo $count
1 Like

This is Useless Use of Cat.

1 Like

Thanks for enlightening me about the spacing and other ways to count lines.