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: Greg Wooledge
Subject: Re: How does quote removal work with alternative forms of parameter expansion?
Date: Sun, 26 May 2024 16:26:35 -0400

On Sun, May 26, 2024 at 06:18:39PM +0200, Philippe Cerfon wrote:
> x="${1##*}"    |  bash x.sh "*bc"   |   <empty>   (i.e. the * is the 
> meta-char)
> x="${1##"*"}"  |  bash x.sh "*bc"   |   bc        (i.e. the * is the

On the right hand side of # or ## you're allowed to have a glob.  The
glob may be contained in a variable, or written out explicitly.

If any of the characters of the glob are quoted, they lose their special
meaning.  This also applies if the glob is in a variable, and the variable
expansion is double-quoted.  In that case, all the characters of the glob
are considered quoted, and therefore non-special.

It's the same treatment you get on the right hand side of = in [[ commands:

    [[ $x = $pattern ]]     # Contents of $pattern are treated as a glob.

    [[ $x = "$pattern" ]]   # Contents of $pattern are treated as a string.

> x="${1:-$bar}"    |  bash x.sh ""   |   expanded
> x="${1:-"$bar"}"  |  bash x.sh ""   |   expanded

Because the right hand side of ## has special globby rules, the right hand
side of - or :- gets treated the same way, to minimize surprises.

Double quotes around $bar do not prevent the variable expansion, but they
would suppress any glob behavior in the result.  Since :- doesn't perform
globbing anyway, you don't see a difference here.  You would see a
difference between

    ${1##$bar}

and

    ${1##"$bar"}

in that the latter would suppress glob matching.

> x="${1:-'$bar'}"  |  bash x.sh ""   |   'expanded'  => why not quote
> removal, why expansion?

Because it's what people expect.  The $ being inside single quotes makes
it a literal $ rather than the beginning of an expansion.



reply via email to

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