Sometimes I have a one-liner that I am repeating many times for a particular task, but will likely never use again in the exact same form. It includes a file name that I am pasting in from a directory listing. Somewhere in between and creating a bash script I thought maybe I could just create a one-liner function at the command line like:
numresults(){ ls "$1"/RealignerTargetCreator | wc -l }
I've tried a few things like using eval, using numresults=function...
, but haven't stumbled on the right syntax, and haven't found anything on the web so far. (Everything coming up is just tutorials on bash functions).
Don't use
ls | wc -l
as it may give you wrong results if file names have newlines in it. You can use this function instead:Quoting my answer for a similar question on Ask Ubuntu:
Try with a semicolon after
wc -l
:You can also count files without
find
. Using arrays,or using positional parameters
To match hidden files as well,
(Subtracting 2 from the result compensates for
.
and..
.)The easiest way maybe is echoing what you want to get back.
Anyway here you can find a good how-to for more advanced purposes