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

Re: optimal expansions?



2024-04-21 19:39:20 +0200, Roman Perepelitsa:
[...]
> I'm curious. Which of these would you use?
> 
>     print rebooting
>     print -r -- rebooting

Probably print -u2 rebooting, though in scripts, I usually
define functions for reporting like:

info warn err() print -ru2 -C1 -- "$@"
die() { err "$@"; exit 1; }
info rebooting

In any case, for fixed strings, whether to use -- (or - which
also works for print)  or not is not relevant as long as the
string doesn't start with "-" (or "+" for some commands). Same
for -r as longer as arguments don't contain backslashes.

The -- is needed when arguments are not known at code writing
time (or are known to be a problem).

> Or these:
> 
>     ls ~
>     ls -- ~
> 
> I prefer the first form in both of these examples, whether in scripts
> or interactively. It's less code, so less time to read and understand.
[...]

Strictly speaking, it should be ls -- ~ as ~ expands to $HOME
which we can't guarantee won't start with - (though $HOME
starting with - would only happen in pathological cases).

-- 
Stephane




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