What does a dollar sign followed by a square bracket $[...] mean in bash?

6,317

Solution 1

You can find old bash source here. In particular I downloaded bash-1.14.7.tar.gz. In the documentation/bash.txt you will find:

Arithmetic Expansion

Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. There are two formats for arithmetic expansion:

     $[expression]

     $((expression))

The references to $[ are gone in doc/bash.html from the bash-doc-2.0.tar.gz download and the NEWS file mentions that:

The $[...] arithmetic expansion syntax is no longer supported, in favor of $((...)).

$((...)) is also the standard syntax for an arithmetic expansion, but may have been added to the standard later than the original Bash implementation.

However, $[...] does still seem to work in Bash 5.0, so it's not completely removed.

Solution 2

In a thread on the GNU bash mailing list, it says that the $[ syntax was an early syntax that was deprecated in favor of $((, since the latter was already used by the Korn shell.

According to this site, the manual for bash 3.2.48 contained a reference to the $[ syntax. So presumably this reference had been removed in 3.2.51.

Share:
6,317

Related videos on Youtube

AmadeusDrZaius
Author by

AmadeusDrZaius

Updated on September 18, 2022

Comments

  • AmadeusDrZaius
    AmadeusDrZaius almost 2 years

    It appears that $[expr] performs arithmetic expansion just like $((expr)). But I can't find any mention of $[ in the bash manual. This command gives no results:

    gunzip -c /usr/share/man/man1/bash.1.gz | grep -E '\$\['
    

    What is this operator and is its behavior standardized anywhere?

    My bash version: GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)

    • Janis
      Janis about 9 years
      To explicitly answer your question about a standard: The (obsolete) bash specific $[...] syntax is non-standard. The $((...)) (that stems from ksh) is POSIX standard.
    • Olivier Dulac
      Olivier Dulac about 9 years
      man pages could contain things like _t_h_i_s (which is then interpreted by the man utility so as to appear differently) so your grep could fail if it happens the thing you search was supposed to be noted like this