help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Which one to use - $@ "$@" $*


From: Greg Wooledge
Subject: Re: Which one to use - $@ "$@" $*
Date: Fri, 23 Apr 2021 12:38:32 -0400

> > From: pauline-galea@gmx.com
> > I need help to resolve this because it is still not
> > clear to me which one to use - $@ "$@" $*

Unquoted $@ and $* are useless.  Never use them for anything.

On Fri, Apr 23, 2021 at 06:17:02PM +0200, Christopher Dimech wrote:
> Looks like you just want to re-pass the arguments to another program (e.g. to
> grep in your case).  In such instance, the procedure is to use "$@"

This is correct.  You usually want "$@" which expands to a list of
arguments that are the same as the positional parameters.  You use this
when you're writing a wrapper script or wrapper function.

cgrep() {
  grep --color=always "$@" | less -R
}

The rest of the time, you want "$*" which expands to a single argument
which is all of the positional parameters joined together, with the
first character of IFS (or a space, if IFS is unset) between them.  You
typically use this form when you're writing a function that logs messages
that the user is passing in, which might be a single word, or multiple
words.  It's a lot like echo.

log() {
  printf '%s: %s\n' "$progname" "$*" >&2
}

That should cover 95% of your needs.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]