Why does the terminal respond with "2" when I use who | wc -l

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

4 Answers

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

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

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like