help-bash
[Top][All Lists]
Advanced

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

Re: sed with Variable Substitution in the command


From: Wiley Young
Subject: Re: sed with Variable Substitution in the command
Date: Wed, 25 Sep 2024 22:12:24 -0700

From: Steve Matzura
  "... Variable substitution has always been a problem for me because there
doesn't seem to be a single hard-and-fast rule--sometimes the variable is
quoted, sometimes it's wrapped in braces...."


Regarding braces, it's always permissible to surround a parameter name in
braces, meaning that adding braces won't cause an error. On the other hand,
there are numerous situations where omitting braces would cause an error.

Okay         ??
====     ====
"${foo}"
            "$foo" is okay
"${bar##_*}"
            "$bar##_*" is an error
"${1}"
            "$1" is okay
"${11}"
            "$11" is an error
"${array[1]}"
             "$array[1]" is an error

I use the term "error" loosely to mean, unintended effects will occur.

Braces are also used for other things as well: compound commands, function
definitions, and brace expansion among others. Each of these are something
else, though.

Just always using braces _is_ extra typing. Some might say that it's less
_readable._ Still, doing so lets users more errors while remembering fewer
rules.

Wiley


reply via email to

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