[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: function that prints each argument on a new line
From: |
Greg Wooledge |
Subject: |
Re: function that prints each argument on a new line |
Date: |
Tue, 6 Jul 2021 08:51:54 -0400 |
On Tue, Jul 06, 2021 at 02:29:41PM +0200, lisa-asket@perso.be wrote:
> From: Greg Wooledge <greg@wooledge.org>
> To: help-bash@gnu.org
> Subject: Re: function that prints each argument on a new line
> Date: 06/07/2021 13:54:01 Europe/Paris
>
> On Tue, Jul 06, 2021 at 01:40:46PM +0200, lisa-asket@perso.be wrote:
> > Would like to have a function that prints a number of lines by passing them
> > as arguments.
>
> f() { printf '%s\n' "$@"; }
>
>
>
> I thought I would have to repeat the format `$#` times. Is that not so?
It's not. The shell's printf has some differences from printf(3).
One of them is that if you provide more arguments than there are
percent-thingies in the format, the format is repeated as many times
as necessary to consume all of the arguments.
Here's the "args" command that I use. It's a script, not a function,
because sometimes I might want to call it with find -exec or with xargs -0
or something. It comes in handy on a surprising number of occasions.
#!/bin/sh
printf "%d args:" "$#"
test "$#" -gt 0 && printf " <%s>" "$@"
echo