use output of command as grep argument

1.8k Views Asked by At

I'm trying to use output of another command as grep argument (I think this feature is known as Bash process substitution). But the problem is that it does not work, grep did not find any entries

wakatana@ubuntu:~$ dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | grep  <(uname -mrs | awk '{print $2}')
wakatana@ubuntu:~$

But when I first assign it to variable then it works:

wakatana@ubuntu:~$ pattern=$(uname -mrs | awk '{print $2}')
wakatana@ubuntu:~$ dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | grep  $pattern
8222    linux-image-4.15.0-118-generic
12894   linux-headers-4.15.0-118-generic
64165   linux-modules-4.15.0-118-generic
165817  linux-modules-extra-4.15.0-118-generic

Output of desired command is following

wakatana@ubuntu:~$ uname -mrs | awk '{print $2}'
4.15.0-118-generic

But when I echo result of process substitiution I get this:

wakatana@ubuntu:~$ echo  <(uname -mrs | awk '{print $2}')
/dev/fd/63

What I'm doing wrong?

OS info:

wakatana@ubuntu:~$ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

wakatana@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.5 LTS
Release:        18.04
Codename:       bionic

wakatana@ubuntu:~$ uname -a
Linux ubuntu 4.15.0-118-generic #119-Ubuntu SMP Tue Sep 8 12:30:01 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
1

There are 1 best solutions below

0
On

You do not want to read from the stream associated with the output of command, you want to pass the whole result of a command as argument to another command. So you want:

dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | grep "$(uname -mrs | awk '{print $2}')"

What I'm doing wrong?

Well, I guess you wrongly expect process substitution to be substituted for the content of the output of a command, while it is being substituted with a single filename associated with the output of the command. To replace the command by the output produced by that command use command substitution. For further research I advise bash manual process substitution, bash manual command substitution.