Up your pipe game with the echo and command substitution trick

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.

Last Updated: 2024-03-05

When you are doing a lot of work with piping, you often wish to prepare a table of output. The problem is that usually shell commands work best with single word outputs (i.e. one column)

A trick to get things done, without resorting to extra variables, by using echo and command substitution.

For example, in the Destroy All Software videos, Gary built a command to show the number of times a class was used in a Ruby codebase. The report needed two columns: the class name, and its count. This was achieved, roughly, with:

find ... | xargs | sed ... | while read class; do echo $(grep $class) $class

The important bit to notice is the combination of echo with a command subtitution $(grep $class) and a variable $class.

For completion's sake, here's how you would achieve the same result using variables. They are assigned in the while loop and also echoed there.

$ for f in levels/* ; do size=$(wc -c <$f) ; data=$(od -tuS -An -N4 $f) ; echo "$size: $data" ; done | less