Conditional pipelining in Bash

Viewed 859

I have a filter that I want to enable optionally and I wonder how can I do this in bash in a clean way.

FILTER="| sort" # also can be empty
ls $FILTER | cat

This code does not work because it will call ls with | and sort as parameters.

How can I do this correctly? Please mind that I am trying to avoid creating if blocks in order to keep the code easy to maintain (my piping chain is considerable more complex than this example)

2 Answers
Related