tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: let and exp shell builtins



    Date:        Tue, 3 Dec 2019 08:29:20 +0000
    From:        Emmanuel Dreyfus <manu%netbsd.org@localhost>
    Message-ID:  <20191203082920.GA6638%homeworld.netbsd.org@localhost>

  | Today I discovered the let and exp shell builtins. They exist in /bin/sh
  | but are only documented in ksh(1).

Incidentally, note that the sh and ksh versions of let (I don't think
/bin/ksh has "exp") are not at all the same.

In ksh each arg is an expression, evaluated one after the other.
In sh, the args are joined (just like echo does, with spaces between)
to form a single expression, and the result is sent to stdout.

ie: in sh
	echo $(( expr ))
and
	let expr

are mnore or less the same thing - differences are that "expr" is not
quoted in any way (by default), with the let/exp form:
	let 3 * 3
will probably not do what was intended, where echo $(( 3 * 3 )) would
(as would let "3 * 3" or let 3 \* 3 or anything similar, of course).

Second the exit status of let reflects the result (that's the same as ksh,
where it reflects the last result).   let (also exp since it is the exact
same thing) does exit(0) if the expression value is not 0, and exit(1) if
it is (and exit(2) on a syntax error, divide by 0, etc).  echo simply
does exit(0) (this is the sole advantage of let/exp over $(( )) evaluation).
[let (and exp in ash) were invented before the $(( )) syntax I believe.]

As for the expression, in sh (and I believe also in ksh) the expression is
exactly the same thing as $(( )) accepts (uses the same code...).

kre



Home | Main Index | Thread Index | Old Index