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 this is a very good summary of what it is, really… I’ve been trying to get more “programming language thinking” into shell anyway, e.g. when I say that “a string is not split if expanded in scalar context” like it’s Perl (but foo=$bar or case $baz in is scalar context)