[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 20:19:38 -0400 |
On Mon, May 27, 2024 at 01:59:54AM +0200, Philippe Cerfon wrote:
> 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"
> }
What I would do is put the default string in a variable (which you can
quote however you need to), and then use that variable in the conditional
expansion.
foo() {
local default_format='some default string with backticks `%s`'
local error1_format="${5:-$default_format}"
printf -- "$error1_format" "$foo"
}
And then if I were really concerned about how it behaves across different
shells and different versions of bash, I'd test it on several of them just
to be sure.
This is because, to me, the rules are so insanely twisted and obscure
that I can't possibly know them all. I rely on "make it as simple as I
can possibly make it, and then test it anyway, because shells are full
of landmines".
One of the possible landmines here is that you've got the word "local"
involved. So this is no longer a basic assignment command. Now it's
a command whose argument simply has an = sign in the middle of it. Some
shells may treat this like an assignment, with all the magic rules that
assignments enjoy, and some may not.
If you wanted to simplify this even further, for purposes of avoiding
landmines, you could separate out the "local":
foo() {
local default_format='some default string with backticks `%s`'
local error1_format
error1_format=${5:-$default_format}
printf -- "$error1_format" "$foo"
}
Now the error1_format=... line is an actual assignment, which means you
can drop the double quotes from around the right hand side, and enjoy
the fact that word splitting and globbing won't happen. You also get
different behavior with regards to exit statuses (and thus with "set -e")
this way, should the assignment happen to contain a command substitution.
I'd probably keep the original version (with local x="$y" which requires
the quotes), but that's a personal decision.
- How does quote removal work with alternative forms of parameter expansion?, Philippe Cerfon, 2024/05/26
- Re: How does quote removal work with alternative forms of parameter expansion?, Lawrence Velázquez, 2024/05/26
- Re: How does quote removal work with alternative forms of parameter expansion?, Philippe Cerfon, 2024/05/26
- Re: How does quote removal work with alternative forms of parameter expansion?, Lawrence Velázquez, 2024/05/26
- Re: How does quote removal work with alternative forms of parameter expansion?, Chet Ramey, 2024/05/29
Re: How does quote removal work with alternative forms of parameter expansion?, Koichi Murase, 2024/05/26