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

Re: Mapping quoted parameter in function



> On 2016 Nov 10 , at 1:20 a, Bernd Steinhauser <linux@xxxxxxxxxxxxxxxxxxxx> wrote:
> 
> Hi,
> 
> I'm using a program that expects a parameter (actually multiple parameters) in the form
> program "foo='bar'"
> 
> Because the cmdline for that program gets quite long, I wrote a function to call it and change parameters easily,it looks roughly like this:
> progfunc() {
> CORES=12
>    program -n ${CORES} "foo='bar'" foo2="1 $3"
> }
> 
> What I would want to do is to ensure that if I call
> `progfunc x`
> 
> this would translate into "foo='x'", without touching the rest of the call.
> Is that somehow possible?
> iirc, variables won't work, because of the quoting style?
> 
> Best Regards,
> Bernd

Just replace bar with a parameter default expansion.

    progfunc () {
      CORES=12
      program -n $CORES "foo='${1:-bar}' foo2="1 $3"
    }

    progfunc              # foo='bar'
    progfunc "hi there"   # foo='hi there'

If you don’t pass a first argument, bar is used. Otherwise, the value of the argument is.

The single quotes here don’t actually quote anything; they are literal characters included in the *double*-quoted string.

Clint


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