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

Using Arithmetic Expansion $((expression)) in a makefile.

662 views
Skip to first unread message

Rolf Campbell

unread,
May 18, 2002, 3:05:14 PM5/18/02
to
I was trying to get (cygwin) make to do some arithmetic. I wanted to
use bash's arhithmetic expansion to do something like:

all:
<tab>echo $((5+2))

Expecting it to look something like this:
echo $((5+2))
7

I know that '$' is a special character and should be escaped, so I
tried:

echo $$((5+2))

Which produced this output (emacs view):
\206 5+2\207

I can't figure out what is going on with that. It looks like some
kind of trigraph expansion, replacing $$(( with 0206 (octal).

I've tried every type of escaping I could think of:
echo \$\$\(\(5+2\)\)
echo \\$$((5+2))

They all get "unexpected )" or something like that depending on the
exact escape I use.

I'm not looking for a work-around (I've already changed it to use
$(shell perl -e "print 5+2") and that works just great). I'd like to
know how to force make to use bash's arithmetic expansion.

-Endlisnis

joe durusau

unread,
May 18, 2002, 5:05:51 PM5/18/02
to

Rolf Campbell wrote:

Well, for a start, make doesn't use bash. I'm told that on
cygwin it uses cmd.exe exclusively. (Never tried to confirm
myself).

Speaking only for myself,

Joe Durusau

Rolf Campbell

unread,
May 18, 2002, 9:50:23 PM5/18/02
to
joe durusau <dur...@bellsouth.net> wrote in message news:<3CE6C22F...@bellsouth.net>...

> Rolf Campbell wrote:
>
> > I was trying to get (cygwin) make to do some arithmetic. I wanted to
> > use bash's arhithmetic expansion to do something like:
> >
> > echo $$((5+2))
> >
> > Which produced this output (emacs view):
> > \206 5+2\207
> >
> > I can't figure out what is going on with that. It looks like some
> > kind of trigraph expansion, replacing $$(( with 0206 (octal).
> >
> > I'm not looking for a work-around (I've already changed it to use
> > $(shell perl -e "print 5+2") and that works just great). I'd like to
> > know how to force make to use bash's arithmetic expansion.
> >
> > -Endlisnis
>
> Well, for a start, make doesn't use bash. I'm told that on
> cygwin it uses cmd.exe exclusively. (Never tried to confirm
> myself).
>
> Joe Durusau

That was the answer. Here's the final results...
<tab>bash -c 'echo $$((5+2))'

output:
bash -c 'echo $((5+2))'
7

-Endlisnis

Soren A.

unread,
May 19, 2002, 4:27:55 PM5/19/02
to
Endl...@yahoo.com (Rolf Campbell) wrote in
news:2e314290.02051...@posting.google.com on 18 May 2002:

> joe durusau <dur...@bellsouth.net> wrote in message
> news:<3CE6C22F...@bellsouth.net>...
>> Rolf Campbell wrote:
>>
>> > I was trying to get (cygwin) make to do some arithmetic. I wanted
>> > to use bash's arhithmetic expansion to do something like:
>> >
>> > echo $$((5+2))
>> >
>> > Which produced this output (emacs view):
>> > \206 5+2\207

{snip}

>> Well, for a start, make doesn't use bash. I'm told that on
>> cygwin it uses cmd.exe exclusively. (Never tried to confirm
>> myself).
>>
>> Joe Durusau
>
> That was the answer.

That certainly may have led you to a result that you wanted but it is
misinformation and somebody will probably pay down the road.

Please read this next part CAREFULLY.

BASH is the default shell for Cygwin, by definition. Cygwin also
includes the 'sh.exe' which is actually a shell named 'ash' from Gnu. By
default, Gnu `make' running on a Cygwin development platform runs in
MAKE MODE (the env var is MAKE_MODE) 'unix' unless you've messed it up
(set it to 'win32'). In the default MAKE_MODE=unix, the shell `make'
uses is *sh.exe*. That's a minimal POSIX-compliant shell with no Gnu
extentions ... yeah, guess what ... no BASH arithmetic expansion.

The way to tell make to use a different shell to build a target is a
valuable trick I figured out recently:

MYTARGET : SHELL=/bin/bash
MYTARGET :
something ... echo $$((5+2)) ...

It appears you found a way to run 'sh' (to have make run sh, which is
what it does naturally) merely to invoke an instance of bash
inside that, thus it worked for you, but at cost of a needless shell
instance (which on win32 is particularly expensive).

Regards,
Soren Andersen
--
# Advanced (or lunatic, depending on your pov) examples of Gmake use:
# || MakefileWorkshop: ||
# http://home.att.net/~perlspinr/makefiles/makefileworkshop.html
#########################################################

0 new messages