0

I'm running this series of commands

passwd=`wc -l /etc/passwd`

echo $passwd

Returns:

34 /etc/passwd

What do I need to do to this so that it will only show the output of wc -l?

Leon
  • 31,443
  • 4
  • 72
  • 97
MRLOBE
  • 3
  • 3

4 Answers4

1

Just read from standard input instead of giving wc a file name:

$ passwd=`wc -l < /etc/passwd`
$ echo "$passwd"
      86

wc still outputs quite a bit of padding, but the file name is omitted (because wc has no idea what file the data comes from).

chepner
  • 497,756
  • 71
  • 530
  • 681
0

That's the default behaviour of wc:

» wc -l /etc/passwd                                     
28 /etc/passwd

There is no way to tell wc not to output the filename.

Ikke
  • 99,403
  • 23
  • 97
  • 120
0

Using awk perhaps ?

$ passwd=$(wc -l /etc/passwd | awk '{print $1}')
$ echo $passwd
32

Using cut, from cut (GNU coreutils)

$ passwd=$(wc -l /etc/passwd | cut -d" " -f1)
$ echo $passwd
32
Inian
  • 80,270
  • 14
  • 142
  • 161
0

wc returns also the filename, but there are other ways to do it. Some examples:

passwd=`wc -l /etc/passwd | grep -o [1-9]\*`

or

passwd=`wc -l /etc/passwd | cut -f1 -d' '`

(answer from this question: get just the integer from wc in bash)

Community
  • 1
  • 1
Vitor Costa
  • 114
  • 1
  • 2
  • 12