Sha256: b42e6534066526f530fb6cc15a5b471b3d0fe7013ad83f5060e02f8c07d8a796
Contents?: true
Size: 1.45 KB
Versions: 10
Compression:
Stored size: 1.45 KB
Contents
The Unix shell does not directly support arithmetic operations, so external tools, such as ''expr'' are used to perform arithmetic calculations when required: {{works with|Bourne Shell}} {{works with|Almquist SHell}} #!/bin/sh read a; read b; echo "a+b = " `expr $a + $b` echo "a-b = " `expr $a - $b` echo "a*b = " `expr $a \* $b` echo "a/b = " `expr $a / $b` # truncates towards 0 echo "a mod b = " `expr $a % $b` # same sign as first operand Notes: Using the ` (backtick operators, also available in most Bourne shells via the ''$(...)'' syntax) allows us to keep the results on their labels in the most efficient and portable way. The spaces around the operators in the ''expr'' command line arguments are required and the shell requires us to quote or escape the ''*'' character has shown, to prevent any possible "globbing" --- filename expansion of the ''*'' as a wildcard character. With SUSv3 parameter expansions: {{works with|Bourne Again SHell|3.2}} {{works with|pdksh|5.2.14}} {{works with|Z SHell}} #!/bin/sh read a; read b; echo "a+b = $((a+b))" echo "a-b = $((a-b))" echo "a*b = $((a*b))" echo "a/b = $((a/b))" # truncates towards 0 echo "a mod b = $((a%b))" # same sign as first operand Note: spaces inside the ''$((...))'' are optional and not required; the ''$((...))'' can be inside or outside the double quotes, but the `...` expressions from the previous example can also be inside or outside the double quotes.
Version data entries
10 entries across 7 versions & 1 rubygems