Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bash operator '$['

67 views
Skip to first unread message

Jon LaBadie

unread,
Nov 21, 2012, 6:23:41 AM11/21/12
to
I have noticed that in bash $[...] appears to be equivalent to $((...)).

I've found no documentation for it and only one reference in the bash changelog.
That was back in a version 2 note about parsing "$..." strings and $[ was included
in the list with ${ and others.

Perhaps $[ was the original response to ksh93's $((. Anyone know?

Thanks,
Jon

Stachu 'Dozzie' K.

unread,
Nov 21, 2012, 6:40:06 AM11/21/12
to
On 2012-11-21, Jon LaBadie <jlab...@aXcXm.org> wrote:
> I have noticed that in bash $[...] appears to be equivalent to $((...)).
>
> I've found no documentation for it and only one reference in the bash changelog.

You have strange documentation. My `man bash' says:

#v+
EXPANSION
[...]
Arithmetic Expansion
Arithmetic expansion allows the evaluation of an arithmetic expression
and the substitution of the result. The format for arithmetic expan‐
sion is:

$((expression))

The old format $[expression] is deprecated and will be removed in
upcoming versions of bash.
#v-

Debian Squeeze, bash 4.1.

--
Secunia non olet.
Stanislaw Klekot

Jon LaBadie

unread,
Nov 21, 2012, 10:34:52 AM11/21/12
to
One of us has :)

I tried to do my due diligence. Looked at the man page on the systems I have
access to (CentOS, Fedora, SLES, Solaris) and at several sets of on-line docs.
Found nothing under Arithmetic Expansion nor ARITHMETIC EVALUATION in any of
them. Also did searches for $[ on each ($[[] for regular expressions or $[
in web browsers). NADA

Thanks

Jon LaBadie

unread,
Nov 21, 2012, 10:38:08 AM11/21/12
to
On 11/21/2012 06:40 AM, Stachu 'Dozzie' K. wrote:

Christian Weisgerber

unread,
Nov 21, 2012, 10:20:26 AM11/21/12
to
Jon LaBadie <jlab...@aXcXm.org> wrote:

> I have noticed that in bash $[...] appears to be equivalent to $((...)).

Very old and long deprecated.

> Perhaps $[ was the original response to ksh93's $((. Anyone know?

Well, $(( was already in ksh88, so the timeline must be different.

--
Christian "naddy" Weisgerber na...@mips.inka.de

Thomas 'PointedEars' Lahn

unread,
Nov 21, 2012, 1:27:14 PM11/21/12
to
Assuming your $MANPAGER (or equivalent) is less(1) (or equivalent), you have
to find a match for one of `\$\[', `[$]\[', and `[$][[]', because unescaped
`$' specifies the end of line/input in regular expressions.

It is not likely that you find that on the web with a search engine; I could
not with Google, even though I double-quoted it (which usually helps). You
can still find it in some shell scripts, though, via
<http://code.google.com/codesearch>.

--
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Christian Weisgerber

unread,
Nov 21, 2012, 4:48:17 PM11/21/12
to
Stachu 'Dozzie' K. <doz...@go.eat.some.screws.spammer.invalid> wrote:

> Arithmetic Expansion
> Arithmetic expansion allows the evaluation of an arithmetic expression
> and the substitution of the result. The format for arithmetic expan-
> sion is:
>
> $((expression))
>
> The old format $[expression] is deprecated and will be removed in
> upcoming versions of bash.
>
> Debian Squeeze, bash 4.1.

That is no longer in the bash 4.2 manual page.

Actually, it isn't in the 4.1 one either. That must be a local
Debian addition.

Chris F.A. Johnson

unread,
Nov 21, 2012, 9:09:44 PM11/21/12
to
It isn't in the bash man pages going back as far as 2.0

Presumably, it's left in the shell for backwards compatibility, but
there's no need for it to be mentioned.


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

Jon LaBadie

unread,
Nov 21, 2012, 11:16:41 PM11/21/12
to
Right you are. I haven't adjusted to the change from '$' being special only
at the ends of REs. However, I did read through the ARITHMETIC EVALUATION
and Arithmetic Expansion sections of each available manpage.

> It is not likely that you find that on the web with a search engine; I could
> not with Google, even though I double-quoted it (which usually helps). You
> can still find it in some shell scripts, though, via
> <http://code.google.com/codesearch>.
>
I used the browser to find bash documentation. Then searched the webpage using
the browser's non-regex search engine using a simple '$['.

Thomas 'PointedEars' Lahn

unread,
Nov 22, 2012, 1:48:36 PM11/22/12
to
When and where has that ever been the case?

> However, I did read through the ARITHMETIC EVALUATION
> and Arithmetic Expansion sections of each available manpage.

Apparently it was removed from some manpages but not others. It is in mine
(GNU bash, version 4.2.37(1)-release (i486-pc-linux-gnu); manpage GNU
Bash-4.2 dated "2010 December 28") the same as quoted above.

>> It is not likely that you find that on the web with a search engine; I
>> could not with Google, even though I double-quoted it (which usually
>> helps). You can still find it in some shell scripts, though, via
>> <http://code.google.com/codesearch>.
>
> I used the browser to find bash documentation. Then searched the webpage
> using the browser's non-regex search engine using a simple '$['.

Curiously enough, apparently neither
<http://tiswww.case.edu/php/chet/bash/bashref.html> nor
<http://tiswww.case.edu/php/chet/bash/bash.html>, of the *same* date, have
it. Neither has bash.1 in <http://ftp.gnu.org/gnu/bash/bash-4.2.tar.gz>.

Jon LaBadie

unread,
Nov 23, 2012, 12:45:53 AM11/23/12
to
On 11/22/2012 01:48 PM, Thomas 'PointedEars' Lahn wrote:
> Jon LaBadie wrote:
>
>> On 11/21/2012 01:27 PM, Thomas 'PointedEars' Lahn wrote:
>>> Jon LaBadie wrote:
>>>>
...
>>>> ... Also did searches for $[ on each ($[[] for
>>>> regular expressions or $[ in web browsers). NADA
>>>
>>> Assuming your $MANPAGER (or equivalent) is less(1) (or equivalent), you
>>> have to find a match for one of `\$\[', `[$]\[', and `[$][[]', because
>>> unescaped `$' specifies the end of line/input in regular expressions.
>>>
>> Right you are. I haven't adjusted to the change from '$' being special
>> only at the ends of REs.
>
> When and where has that ever been the case?
>

Until when I know not, but it was true in basic REs since the dawn of grep
and ed and in extended REs as used by egrep and {n}awk.

From current regex(7):

Obsolete ("basic") regular expressions differ in several respects.
...
... , '$' is an ordinary character except at the end of the RE
...

Thomas 'PointedEars' Lahn

unread,
Nov 23, 2012, 6:21:33 AM11/23/12
to
Jon LaBadie wrote:

> On 11/22/2012 01:48 PM, Thomas 'PointedEars' Lahn wrote:
>> Jon LaBadie wrote:
>>> […] I haven't adjusted to the change from '$' being special
>>> only at the ends of REs.
>> When and where has that ever been the case?
>
> Until when I know not, but it was true in basic REs since the dawn of grep
> and ed and in extended REs as used by egrep and {n}awk.
>
> From current regex(7):
>
> Obsolete ("basic") regular expressions differ in several
> respects.
> ...
> ... , '$' is an ordinary character except at the end of the RE
> ...

Thank you, somehow I missed that. However, it should be noted that it
continues with

| […] or(!) the end of a parenthesized subexpression, […]
0 new messages