[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: sed with Variable Substitution in the command
From: |
Andreas Kähäri |
Subject: |
Re: sed with Variable Substitution in the command |
Date: |
Thu, 26 Sep 2024 20:03:10 +0200 |
On Thu, Sep 26, 2024 at 01:16:21PM -0400, Greg Wooledge wrote:
> On Thu, Sep 26, 2024 at 18:53:59 +0200, alex xmb sw ratchev wrote:
> > On Thursday, September 26, 2024, Wiley Young <wyeth2485@gmail.com> wrote:
> > > "${1}"
> > > "$1" is okay
> > > "${11}"
> > > "$11" is an error
>
> > sorry .. where is $11 and error ?
>
> $11 would be equivalent to ${1}1 which is not what's intended.
How do you know it's not intended? ;-) "Intention" is not something that
can be inferred from the syntax of small fragment of a shell script.
Likewise, with the examples "$bar##_*" and "$array[1]" in the previous
messages; these are not _incorrect_ or _errors_ in themselves, nor can
we say that they are _intended_ to be something else without knowing the
context in which they are used.
I'm in the camp that says to use curly braces around variable names when
there is an ambiguity, or when a particular substitution is desired.
But to leave them out when all you want is the variable's value and the
name is not otherwise concatenated with other characters in a string.
It's better to know the rules. Just saying "always use curly braces"
feels cargo cult-ish to me, and then you end up with people thinking
that using curly braces is the same as "quoting the variable".
But I understand that after decades of seeing people make the same
mistakes over and over again, it's easy to get a bit tired of it, and
just serve up simple rules of thumb.
--
Andreas (Kusalananda) Kähäri
Uppsala, Sweden
.
- Re: sed with Variable Substitution in the command, (continued)
- Re: sed with Variable Substitution in the command, Wiley Young, 2024/09/26
- Re: sed with Variable Substitution in the command, alex xmb sw ratchev, 2024/09/27
- Re: sed with Variable Substitution in the command, Andreas Kähäri, 2024/09/27
- Re: sed with Variable Substitution in the command, alex xmb sw ratchev, 2024/09/27
- Re: sed with Variable Substitution in the command, Andreas Kähäri, 2024/09/27
- Re: sed with Variable Substitution in the command, Greg Wooledge, 2024/09/27
- Re: sed with Variable Substitution in the command, alex xmb sw ratchev, 2024/09/27
- Re: sed with Variable Substitution in the command, alex xmb sw ratchev, 2024/09/27