Piping to the command substitution of a string containing pipes
When you want to run variables that contain code you'll often want to use the command eval
. This will expand the contents of the variable so that they can be executed.
Example
$ x='grep a | grep b'
$ echo ab | eval "$x"
ab
Using eval is often discourage though, so use caution, see this BashFAQ titled: Eval command and security issues for more examples!
References
- Execution of a command in a variable with eval
Ah, I figured it out after some experimentation—use alias
—
$ x='grep a | grep b'
$ alias y=$x
$ echo ab | y
ab
Please do post any other ways to do this—I'd be interested in alternatives.