help-bash
[Top][All Lists]
Advanced

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

Re: How does quote removal work with alternative forms of parameter expa


From: Philippe Cerfon
Subject: Re: How does quote removal work with alternative forms of parameter expansion?
Date: Mon, 27 May 2024 01:59:54 +0200

Hey again.

On Mon, May 27, 2024 at 1:38 AM Greg Wooledge <greg@wooledge.org> wrote:
> hobbit:~$ set -- foo; bar=BAR
> hobbit:~$ echo "${1:-'$bar'}"
> foo
> hobbit:~$ set --
> hobbit:~$ echo "${1:-'$bar'}"
> 'BAR'
>
> OK, I don't know.  I don't try to figure out all of the corner cases.
> What I try to do is figure out how to write scripts that actually work.
...

> So, the question becomes "What do you want to do?"

It's less a matter of getting something to work, I merely stumbled
over this - to me unreasonable - behavior.
What I did was allowing the caller of some function to adapt the error
message used within that function like so:
foo()
{
   local error1_format_string="${5:-'some default string with backticks `%s`'}"
  #later if some error condition is true, it would
  printf "$error1_format_string" "$foo"
}

I noticed that this fails awkwardly, which is because the inner single
quotes do not actually seem to quote here, but the backticks are
evaluated as command substitution. It will work if I double quote or
backslash quote the backticks.

Still, my understanding was always naively that the word in
${var+word} or ${var%word} (and the others like :+ - % ## etc) works
in both like another level of quoting, like within a command
substitution (regardless of whether or not the whole parameter
expansion is itself (double)quoted or not.

While it seems to work like that for # ## % and %% it turned out it
doesn't for the other forms.
So now I a) try to understand the logic behind what happens, so far no
success, and b) wonder how many of my scripts are subtly broken
because I always assumed the behavior to be like "two levels of
quoting".

Thanks,
Philippe.



reply via email to

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