I'm looking for a way to use the ouput of a command (say command1) as an argument for another command (say command2).
I encountered this problem when trying to grep
the output of who
command but using a pattern given by another set of command (actually tty
piped to sed
).
Context:
If tty
displays:
/dev/pts/5
And who
displays:
root pts/4 2012-01-15 16:01 (xxxx)
root pts/5 2012-02-25 10:02 (yyyy)
root pts/2 2012-03-09 12:03 (zzzz)
Goal:
I want only the line(s) regarding "pts/5"
So I piped tty
to sed
as follows:
$ tty | sed 's/\/dev\///'
pts/5
Test:
The attempted following command doesn't work:
$ who | grep $(echo $(tty) | sed 's/\/dev\///')"
Possible solution:
I've found out that the following works just fine:
$ eval "who | grep $(echo $(tty) | sed 's/\/dev\///')"
But I'm sure the use of eval
could be avoided.
As a final side node: I've noticed that the "-m" argument to who
gives me exactly what I want (get only the line of who
that is linked to current user). But I'm still curious on how I could make this combination of pipes and command nesting to work...