Sometimes you need to make a small addition to a big command pipe
The OP's "Good, but verbose" example is actually pretty awesome for many cases.
Please forgive the contrived example. The whole reason I need such a solution is that I have a lot of distracting, nested code. But, it boils down to: I must do a !d
in sed
within a double quoted bash command expansion.
This works
$ ifconfig | sed '/inet/!d'
inet 127.0.0.1 netmask 0xff000000
…
This does not
$ echo "$(ifconfig | sed '/inet/!d')"
-bash: !d': event not found
This is a simplest compromise
$ echo "$(ifconfig | sed '/inet/'\!'d')"
inet 127.0.0.1 netmask 0xff000000
…
Using the compromise allows me to insert a few characters into the existing code and produce a Pull Request that anyone can understand… even though resulting code is more difficult to understand. If I did a complete refactor, the code reviewers would have a much more challenging time verifying it. And of course this bash has no unit tests.