Current-Users archive

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

Re: Proposed updated sh(1) relating to $(( ))



On Sat, Mar 18, 2017 at 9:09 PM, Robert Elz <kre%munnari.oz.au@localhost> wrote:
> This is what I am proposing to have sh(1) say about arithmetic
> expansion ( $(( )) ).   Much of it is the same as is there now.
>
> Any comments?
>
>    Arithmetic Expansion
>      Arithmetic expansion provides a mechanism for evaluating an arithmetic
>      expression and substituting its value.  The format for arithmetic
>      expansion is as follows:
>
>            $((expression))
>
>      The expression is treated as if it were in double quotes, except that a
>      double quote inside the expression is not treated specially.  The shell
>      expands all tokens in the expression for parameter expansion, command
>      substitution, and quote removal.
>
>      Next, the shell treats this as an arithmetic expression and substitutes
>      the value of the expression.
>
>      Arithmetic expressions use a syntax similar to that of the C language,
>      and are evaluated using the `intmax_t' data type (this is an extension to
>      POSIX, which requires only `long' arithmetic).  Shell variables may be
>      referenced by name inside an arithmetic expression, without needing a
>      ``$'' sign.  Variables that are not set, or which have an empty (null
>      string) value, used this way evaluate as zero (that is, ``x'' in
>      arithmetic, as an R-Value, is evaluated as ``${x:-0}'') unless the sh -u
>      flag is set, in which case a reference to an unset variable is an error.
>      Note that unset variables used in the ${var} form expand to a null
>      string, which might result in syntax errors.  Referencing the value of a
>      variable which is not numeric is an error.
>
>      All of the C expression operators applicable to integers are supported,
>      and operate as they would in a C expression, except the unary ``++'' and
>      ``--'' operators (in both prefix and postfix forms) and the ``,'' (comma)
>      operator, which are currently not supported.  It should not be necessary
>      to state that the C operators which operate on, or produce, pointer
>      types, are not supported.  Those include unary ``*'' and ``&'' and the
>      struct and array referencing binary operators: ``.'', ``->'' and ``[''.



I know it's redundant but because man pages tend to be searched rather
than read, I would like to see this paragraph:
     The expression is treated as if it were in double quotes, except that a
     double quote inside the expression is not treated specially.  The shell
     expands all tokens in the expression for parameter expansion, command
     substitution, and quote removal.

Read like this:
     In Arithmetic Expansion the expression is treated as if it were
in double quotes, except that a

So when I run: man sh
and then search for "quotes" I don't get lost

---

I didn't know this:
Shell variables may be referenced by name inside an arithmetic
expression, without needing a ``$'' sign.

So bonus for reading this email :)


Home | Main Index | Thread Index | Old Index