For testing purpose, I want to activate or not a filtering with the command head.
For example, the command below could outputs a lot of lines
#!/bin/bash
produce_a_lot_of_lines
I want to set on demand a filter with head, depending on a variable. A way The sample below works:
#!/bin/bash
function filter() {
[[ "$HEAD" =~ [0-9]+ ]] && head -n $HEAD || cat
}
produce_a_lot_of_lines | filter
Is there a cleaner/more efficient/better way to achieve this?
if [[ "$HEAD" =~ ^[0-9]+$ ]]; then PaLoL | head -n "$HEAD"; else PaLoL; fiPaLoL, but avoid additionalcat.