[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bash let function
From: |
Emanuele Torre |
Subject: |
Re: Bash let function |
Date: |
Mon, 24 Apr 2023 14:57:21 +0200 |
User-agent: |
Mutt/2.2.10 (2023-03-25) |
On Mon, Apr 24, 2023 at 11:51:24AM +0200, Gianluca Graziadei S308963 wrote:
> Dear all,
> for academic use I need to obtain the code of let.c (builtin function).
> The main idea is to improve (if possible) the let shell func to accept also
> the floating point type and develop the support for sin(), cos(), sqrt() and
> other Math.h's functions.
>
> I have downloaded the stable bash release but I have found let.def file (no
> let.c), can you help me ?
Hi.
let.def is the source code file for the let builtin, builtins are
defind in a .def file that contains both their documentation and their
source code; a step of the build process will process that .def file to
make generate a C source file for the builtin, documentation, and the
code necessary to embed the builtin into bash.
If you really want to get the C file generated by the builtin process,
you could run make -C builtins mkbuiltins let.c but there is really
not much of a point in doing that since it is just let.def with the
documentation and licence at the top removed.
Your misunderstanding seems to be that the code that handles arithmetic
expressions like let '1 + 2' is in let.def/let.c; that is not the
case. Arithmetic expressions are not only used by let, they are used by
many things other things in the shell:
# Artihmetic Expansion
printf '1 + 2 = %s\n' "$(( 1 + 2 ))"
# (( ... )) compound command, basically the same as let, but neater
(( a = 10 + 2 ))
if (( a > b )); then echo hi; else echo bye; fi
# array subscripts
arr=( foo bar baz hi ) i=2
printf '%s\n' "${arr[i]}" # baz
printf '%s\n' "${arr[i - 2]}" # foo
# "Substring" PE
str='Hello, friend!'
printf '%s\n' "${str:2 + 3:123 - 118}" # , fri
# ...
let is basically just an eval-like command that takes a string argument,
and evaluates it as an arithemtic expression, returning failure if the
expression evaluates to 0, and returning success otherwise.
Arithmetic expression parser and evaluator defined in expr.c if you
want to extend the functionality of bash's arithmetic expressions, that
is the file you should be editing.
Buona giornata. :)
emanuele6