Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: print built-in doesn't print this.



On 11/14/21, Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> On Sun, 2021-11-14 at 11:19 -0800, jdh wrote:
>> I wanted to insert a string of '-'s to visually show a change in a list.
>> But  I found out that
>>
>>      print "--------"
>>
>> Does not print anything out. Well maybe a \n.
>> Is this intentional?  Or a well known feature?
>
> print has standard option syntax, so you need to terminate
> lists of options (even if they're empty) with "--".
>
> print -- "--------"
>
> The echo builtin is non-standard and doesn't have this
> requirement, but it's non-standard in a different way in
> different shells, so I'd recommend getting used to using
> print like that.

Both print and echo accept a single - to terminate the option list,
while print also accepts the double -- as seen above. echo will just
print any unknown option, while print will complain about most of them
(for some reason, -0 through -9 are just printed verbatim).

If you want to be somewhat portable across shells, the printf builtin
is usually pretty reliable. Although in many cases the format string
cannot begin with a - there either, -- seems universally accepted (I
tested bash, dash, zsh and gnu /usr/bin/printf).

PS

I use this,
alias pl='print -rl -'
which will print its arguments terminated by newlines, and the -r
means to not interpret any escapes. I think
alias pl='printf -- %s\\n'
would be more or less equivalent.

-- 
Mikael Magnusson




Messages sorted by: Reverse Date, Date, Thread, Author