• Uncategorized

About bash : Use-output-of-bash-command-with-pipe-as-a-parameter-for-another-command

Question Detail

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).


If tty displays:


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)


I want only the line(s) regarding “pts/5”
So I piped tty to sed as follows:

$ tty | sed 's/\/dev\///'


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…

Question Answer

One usually uses xargs to make the output of one command an option to another command. For example:

$ cat command1

echo "one"
echo "two"
echo "three"

$ cat command2

printf '1 = %s\n' "$1"

$ ./command1 | xargs -n 1 ./command2
1 = one
1 = two
1 = three

But … while that was your question, it’s not what you really want to know.

If you don’t mind storing your tty in a variable, you can use bash variable mangling to do your substitution:

$ tty=`tty`; who | grep -w "${tty#/dev/}"
ghoti            pts/198  Mar  8 17:01 (:0.0)

(You want the -w because if you’re on pts/6 you shouldn’t see pts/60’s logins.)

You’re limited to doing this in a variable, because if you try to put the tty command into a pipe, it thinks that it’s not running associated with a terminal anymore.

$ true | echo `tty | sed 's:/dev/::'`
not a tty

Note that nothing in this answer so far is specific to bash. Since you’re using bash, another way around this problem is to use process substitution. For example, while this does not work:

$ who | grep "$(tty | sed 's:/dev/::')"

This does:

$ grep $(tty | sed 's:/dev/::') < <(who)

You can do this without resorting to sed with the help of Bash variable mangling, although as @ruakh points out this won’t work in the single line version (without the semicolon separating the commands). I’m leaving this first approach up because I think it’s interesting that it doesn’t work in a single line:

TTY=$(tty); who | grep "${TTY#/dev/}"

This first puts the output of tty into a variable, then erases the leading /dev/ on grep’s use of it. But without the semicolon TTY is not in the environment by the moment bash does the variable expansion/mangling for grep.

Here’s a version that does work because it spawns a subshell with the already modified environment (that has TTY):

TTY=$(tty) WHOLINE=$(who | grep "${TTY#/dev/}")

The result is left in $WHOLINE.

@Eduardo’s answer is correct (and as I was writing this, a couple of other good answers have appeared), but I’d like to explain why the original command is failing. As usual, set -x is very useful to see what’s actually happening:

$ set -x
$ who | grep $(echo $(tty) | sed 's/\/dev\///')
+ who
++ sed 's/\/dev\///'
+++ tty
++ echo not a tty
+ grep not a tty
grep: a: No such file or directory
grep: tty: No such file or directory

It’s not completely explicit in the above, but what’s happening is that tty is outputting “not a tty”. This is because it’s part of the pipeline being fed the output of who, so its stdin is indeed not a tty. This is the real reason everyone else’s answers work: they get tty out of the pipeline, so it can see your actual terminal.

BTW, your proposed command is basically correct (except for the pipeline issue), but unnecessarily complex. Don’t use echo $(tty), it’s essentially the same as just tty.

You can do it like this:

tid=$(tty | sed 's#/dev/##') && who | grep "$tid"

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.