2

The title says it all. I'm the only user on my computer, but when I type in who | wc -l, it responds with 2. Why is that?

3
  • who displays each "terminal" (in a loose sense) loggued in (and what user was used to log in on each). You have 2 terminals Mar 31, 2014 at 13:28
  • 9
    This is a very odd question (analogous of "why do I get two when I add one and one?"). Why does wc -l reply 2? Because you passed it two lines and asked it to count lines. wc is just doing its job. A much better question is why who has two lines of output.
    – Oli
    Mar 31, 2014 at 13:34
  • 9
    Why didn't it occur to you to check out the output of who and see that it outputs two lines?
    – user263910
    Mar 31, 2014 at 14:27

4 Answers 4

5

This is because an open terminal (physical or virtual) is counted as a user logged in. So when you log-in to the account, you log-in once, when you open your terminal, either using Ctrl+Alt+t(hereon called as a virtual terminal) or using Ctrl+Alt+F{1-7}(hereon called as a physical terminal), you log-in once again, each time you log-in.

This is clearly shown in the output of who. The second column shows how the user has logged in. In case you have logged in using the virtual terminal, you will see pts/<no> which represents you have used /dev/pts to log in, which is used to log in using the virtual terminal. If you log-in using the physical terminal, you will see tty<no>, which represents you have used /dev/tty to log-in.

To clarify this, see the following situation:

I have logged in once using the graphical interface, once using the virtual terminal, once using the physical terminal, so the output is:

jobin    tty1         2014-03-31 18:05
jobin    :0           2014-03-31 16:58 (:0)
jobin    pts/1        2014-03-31 17:25 (:0)

The first line indicates the physical terminal login, the second one the graphical and the third one the virtual one.

So the reason you see "2" is because you have logged in to the graphical interface as well as a terminal(not pretty sure whether you have a physical or virtual one).

3
  • Thank you for understanding the question and giving a comprehensible answer.
    – Mdomin45
    Mar 31, 2014 at 12:45
  • 1
    That was pretty fast! :D Did you actually read the answer?
    – jobin
    Mar 31, 2014 at 12:45
  • 1
    Yes, I read the entire thing. It makes a lot more sense now. I had opened up the virtual terminal, thus it shows both tty7 and pts/0.
    – Mdomin45
    Mar 31, 2014 at 12:53
4

who gives the detailed information of currently logged in users one on each line, as following

aditya@aditya-desktop:~$ who
aditya   tty7         2014-03-31 16:45 (:0)
aditya   pts/2        2014-03-31 17:51 (:0)
aditya@aditya-desktop:~$ 

wc -l gives no of lines. In who|wc -l we have piped the commands. That is, we forward the output of who to wc -l command. wc -l will measure the no of lines in the output given by who command, which is 2.

aditya@aditya-desktop:~$ who|wc -l
2
aditya@aditya-desktop:~$ 

For more info, you can read the manuals by typing man who and man wc

7
  • Right, I understand all of that except for the end. Why are two users online? Do I somehow log into two seperate accounts when I get on? Or is there some other arbitrary "user"?
    – Mdomin45
    Mar 31, 2014 at 12:31
  • @user3046635 click ctrl+f1 and ctrl+f7. you will understand. Mar 31, 2014 at 12:32
  • While in the terminal? Pressing ctrl+f1 in the terminal doesn't appear to do anything, and pressing ctrl+f7 inputs ;5~, which I don't understand, and pressing return will just return a syntax error because of the semi-colon.
    – Mdomin45
    Mar 31, 2014 at 12:36
  • it was a typo. you have to type ctrl+alt+f1 and ctrl+alt+f7 Mar 31, 2014 at 12:48
  • @user3046635 ^^ Mar 31, 2014 at 12:54
2

From info coreutils 'who invocation':

   If given no non-option arguments, `who' prints the following
information for each user currently logged on: login name, terminal
line, login time, and remote hostname or X display.

So if you are logged on more than one place and nobody else is logged, the output of who | wc -l will be the number of the places where you are logged. If you open more terminal windows, you will see that this number will increase. The same thing if you open more tabs in a terminal window.

If you are the only user on your computer and if you want to see the number of the unique users currently logged in your system you can use:

who is using_my_pc | wc -l

No, that's a joke (which is funny because it's working in this case). See info coreutils 'who invocation' to see exactly what it means. The right way to see the number of the unique users currently logged in your system is:

who | cut -d' ' -f1 | uniq | wc -l
3
  • @Jobin Which one: who is using_my_pc ? Mar 31, 2014 at 13:31
  • The who | cut -d' ' -f1 | uniq | wc -l one.
    – jobin
    Mar 31, 2014 at 13:31
  • +1, I just loved the who is using_my_pc trick. It quite follows the logic of other commands like whoami, which are meant to be easily readable by humans. Mar 31, 2014 at 20:18
1

who command shows information about the users who are currently logged in.

avinash@avinash-Lenovo-IdeaPad-Z500:~$ who
avinash  tty7         2014-03-31 16:52 (:0)
avinash  pts/1        2014-03-31 17:59 (:0)

tty7 represents the above user is running an X GUI session.And also the second one, pts/1 represents the above user is also running an gnome-terminal session.

Like @Aditya said wc -l counts the number of lines in the standard input.who | wc -l in this command, the output of who command was fed as input to the second wc -l command.Thus inturn, wc -l calculates the number of lines present in the standard input(2) and displays(stdout) the final result.

To see the number of users who are logged in, run who command with -q parameter as below.

avinash@avinash-Lenovo-IdeaPad-Z500:~$ who -q
avinash avinash avinash
# users=3

From man who

-q, --count
          all login names and number of users logged on
1
  • My question is this: why does it say two when there is clearly only one user logged into the system? Am I missing something?
    – Mdomin45
    Mar 31, 2014 at 12:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .