1

Can someone explain why the two commands below give different results?

The first pipes the output of who to grep, the second takes the output of who as a redirection.

~: who |grep $(tty|cut -c 6-)
my_username tty1         2016-01-22 09:56 (:0)

~: grep $(tty|cut -c 6-) <(who)
my_username pts/5        2016-01-22 11:35 (:0.0)

Using color=always as an option to grep in both cases gives the first as matching on tty. The second one matches on pts/5 as expected.

The tty | cut -c 6- command gives me (in this instance) the pts/5 part of /dev/pts/5.

I've tried using quotes (both ' and ") around various parts but with no success.

I realise now that the who -m command gives the line I was after but the question of why the lines above are different still stands.

1 Answer 1

0

The focus is when the shell executes the statement $(...).

 echo $(tty)       # answers me for example /dev/pts/xx
 who | echo $(tty) # answers me `not a tty`

So in a pipe process the current version of bash (GNU bash, version 4.3.11) calls another subshell.
You can read more on the Bash Guide for Beginners [1],[2] .

Do not let the title mislead you... it's full of good information not only for the beginners.

You must log in to answer this question.

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