Mail Archives: cygwin/2005/12/10/16:37:28
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
http://cygwin.com/acronyms/#PPIOSPE
Ugh - top-posted. Reformatted. http://cygwin.com/acronyms/#TOFU
>> From: Eric Blake <ebb9 AT byu DOT net>
^^^^ ^^^^^
Ugh - http://cygwin.com/acronyms/#PCYMTNQREAIYR
>
> If you use bash, $(( x - y )) does just that.
>
>> (nor how to get
>> clock-ticks since 1970/1/1)
>
> date +%s
>
According to Robert Body on 12/10/2005 2:23 PM:
> Thanks Eric, that does the trick, it's short and interesting syntax with
> ((x-y)), i tried (x-y) before myself, i didn't think of trying double
> brackets:
> /home/Owner/r> start=$(date +%s)
> /home/Owner/r> end=$(date +%s)
> /home/Owner/r> (($end-$start))
> /home/Owner/r> alpha=$(($end-$start)); echo $alpha
> 10
>
> how come straight out (($end-$start)) doesn't work, i have to assign it
> to a variable?
The syntax for arithmetic substitution is $(( expression )). It is
required by POSIX shells, and all it does is replace everything from the
starting $(( to the ending )) with the string result, provided the
expression could be evaluated. (($end-$start)) is a bash extension which
is only used to test the result for 0/non-zero, rather than actually
providing the result in the text of the rest of the line.
> with some playing:
> /home/Owner/r> echo (($end-$start))
> bash: syntax error near unexpected token `('
Because (( )) only makes sense as an entire command, not as an argument to
some other command.
> /home/Owner/r> echo `(($end-$start))`
>
Because (()) was used as an entire command, but produced no output, so the
`` was invoked with no input, and produced no output, so echo just printed
a newline.
> /home/Owner/r> echo `($end-$start)`
> bash: 1134249426-1134249416: command not found
>
Because () invokes a subshell, rather than arithmetic substitution, and as
bash told you, there really isn't a command by that name.
> /home/Owner/r> echo $(($end-$start))
> 10
Bingo.
I would suggest reading up on 'man bash'.
>
> too bad it's not a standard way that would work in anything, sh, ksh,
> how would ksh syntax look like?
If your shell is POSIX-compliant (cygwin's /bin/sh, bash, ksh, and zsh all
have POSIX-compliant behavior, but ash does not), then $(( )) will work.
If you want to be portable to older hosts or shells, consider reading up
on 'man expr' (shell arithmetic substitution was designed to be more
efficient than what expr could do).
- --
Life is short - so eat dessert first!
Eric Blake ebb9 AT byu DOT net
volunteer cygwin bash maintainer
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFDm0p284KuGfSFAYARAsG9AKDQG/we1dmLKGYqUqT9JC7wz03WHwCgrhve
M/YdH20NtyJdJzXegck1FrU=
=ycN6
-----END PGP SIGNATURE-----
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -