File this under #shell #functions I should have written years ago:

function grepc { #Do a grep -c, but skipping files with no results grep -c "$@" |grep -v ':0$' }

#unix #UnixShell #ShellScripting #bash #ksh

@rl_dane you need "$@"

@mirabilos

Really? Even if I'm supplying options like grepc -i foo *.md?
Or should I use IFS=$'\n'?

Seems you were right! I need to skim that part of the manpage again. 😋

@rl_dane yes. And absolutely no, do not touch IFS for this.

"$@" basically means “pass the argv array as-is”.

@mirabilos

What happens if you forget the double quotes?

@rl_dane it becomes equivalent to $* without double quotes

@mirabilos

Interesting, that feels kinda counter-intuitive, since not quoting things usually keeps them separate, and quoting them lumps them together, like:

~ $ string="hello there" ~ $ for x in $string; do echo $x; done hello there ~ $ for x in "$string"; do echo $x; done hello there ~ $
@rl_dane yes, but "$@" is special, just learn it as a term of its own
@mirabilos @rl_dane

it's easier to think of $@ as an array type to me

if you quote a string, it goes as is, if you don't, it is split
if you quote an array, it goes as is, if you don't, each entry is split

this would feel a lot less "special case-y" if we had proper arrays variables other than arguments... i'm working on it...
@navi @rl_dane well it’s the same for ${arr[*]} vs. ${arr[@]}
@navi @rl_dane so the @ means “the array itself” vs. * “all array members as a list of (IFS0-joined) words”
@navi @rl_dane so if you do $* it doesn’t just split all array elements, it joins them first then splits the result (this is true)