48

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)

ilkkachu
  • 138,973
  • 5
    To explicitly answer your question about a standard: The (obsolete) bash specific $[...] syntax is non-standard. The $((...)) (that stems from ksh) is POSIX standard. – Janis Jun 15 '15 at 16:20
  • 2
    With Ubuntu 11.04: man bash | grep -E '\$\['. Output: The old format $[expression] is deprecated and will be removed in upcoming versions – Cyrus Jun 15 '15 at 17:17
  • 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 – Olivier Dulac Jun 16 '15 at 13:57

2 Answers2

35

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.

30

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.