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

Re: trying to create a "|| failed" function



On 2013-03-14 at 23:19 -0400, TJ Luoma wrote:
> OUTPUT=`$@ 2>&1`

>  # failed
> echo "$NAME [$TIME]: $@ failed\nOUTPUT: >$OUTPUT<\nExit: >$EXIT<" | tee -a
> "$LOG"

> Anything I can or "should" do differently in the function above?

OUTPUT=`"$@" 2>&1`

Because: $@ drops empty elements, so passing empty parameters to a
command will be dropped, which leads to interesting debugging sessions.

Then in the echo line I quoted, you probably want $* instead of $@
because this is an echo message and you don't want it split apart.

% foo() { echo "snert $@ ni" }
% foo alpha beta
snert alpha ni snert beta ni
%

That's caused by "setopt rc_expand_param" being turned on.

So: $@ is a good habit to get into using, but you usually want "$@",
even in zsh, and for echo situations, you still want $*.

-Phil



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