help-bash
[Top][All Lists]
Advanced

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

How does quote removal work with alternative forms of parameter expansio


From: Philippe Cerfon
Subject: How does quote removal work with alternative forms of parameter expansion?
Date: Sun, 26 May 2024 18:18:39 +0200

Hey.

I'm a bit lost to understand how quoting works with the word in
alternative forms of parameter expansion.

Having a x.sh like:
bar=expanded
foo() {
        x="${1##*}"
        printf '%s\n' "$x"
}
foo "$1"

And calling that with different RHS for the x= I get the following:
     x         |   invocation       |   output
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
literal char)
x="${1##'*'}"  |  bash x.sh "*bc"   |   bc        (i.e. the * is the
literal char)
x="${1##$'*'}" |  bash x.sh "*bc"   |   bc        (i.e. the * is the
literal char)
x="${1##\*}"   |  bash x.sh "*bc"   |   bc        (i.e. the * is the
literal char)

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
literal char)
x=${1##'*'}    |  bash x.sh "*bc"   |   bc        (i.e. the * is the
literal char)
x=${1##$'*'}   |  bash x.sh "*bc"   |   bc        (i.e. the * is the
literal char)
x=${1##\*}     |  bash x.sh "*bc"   |   bc        (i.e. the * is the
literal char)

The same I get with dash (except for the cases with $'', which dash
doesn't support).

So it seems as if there'd be first a "normal" quote removal on the
word part of these parameter expansion forms and then the outer quote
removal, right?


But now, using one of the :- :+ etc forms:
     x            |   invocation    |   output
x="${1:-$bar}"    |  bash x.sh ""   |   expanded
x="${1:-"$bar"}"  |  bash x.sh ""   |   expanded
x="${1:-'$bar'}"  |  bash x.sh ""   |   'expanded'  => why not quote
removal, why expansion?
x="${1:-$'$bar'}" |  bash x.sh ""   |   expanded    => why expansion?
x="${1:-\$bar}"   |  bash x.sh ""   |   $bar

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

Similarly:
$ printf '%s\n' "${some_unset_var:-foo bar}"
foo bar
$ printf '%s\n' "${some_unset_var:-"foo bar"}"
foo bar

$ printf '%s\n' "${some_unset_var:-'foo bar'}"
'foo bar'
=> why not quote removal?

$ printf '%s\n' "${some_unset_var:-$'foo bar'}"
foo bar

$ printf '%s\n' "${some_unset_var:-foo\ bar}"
foo\ bar
=> why not quote removal?


$ printf '%s\n' ${some_unset_var:-foo bar}
foo
bar
=> still expected

$ printf '%s\n' ${some_unset_var:-"foo bar"}
foo bar
$ printf '%s\n' ${some_unset_var:-'foo bar'}
foo bar
$ printf '%s\n' ${some_unset_var:-$'foo bar'}
foo bar
$ printf '%s\n' ${some_unset_var:-foo\ bar}
foo bar
=> contrary to the above, always with quote removal


Thanks,
Philippe



reply via email to

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