]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/let.def
Bash-4.3 patch 46
[thirdparty/bash.git] / builtins / let.def
CommitLineData
726f6388
JA
1This file is let.def, from which is created let.c.
2It implements the builtin "let" in Bash.
3
3185942a 4Copyright (C) 1987-2009 Free Software Foundation, Inc.
726f6388
JA
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
3185942a
JA
8Bash is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
726f6388 12
3185942a
JA
13Bash is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
726f6388 17
3185942a
JA
18You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
726f6388
JA
20
21$BUILTIN let
22$FUNCTION let_builtin
23$PRODUCES let.c
24$SHORT_DOC let arg [arg ...]
3185942a
JA
25Evaluate arithmetic expressions.
26
27Evaluate each ARG as an arithmetic expression. Evaluation is done in
28fixed-width integers with no check for overflow, though division by 0
29is trapped and flagged as an error. The following list of operators is
30grouped into levels of equal-precedence operators. The levels are listed
31in order of decreasing precedence.
726f6388 32
7117c2d2
JA
33 id++, id-- variable post-increment, post-decrement
34 ++id, --id variable pre-increment, pre-decrement
ccc6cda3
JA
35 -, + unary minus, plus
36 !, ~ logical and bitwise negation
7117c2d2 37 ** exponentiation
ccc6cda3
JA
38 *, /, % multiplication, division, remainder
39 +, - addition, subtraction
40 <<, >> left and right bitwise shifts
41 <=, >=, <, > comparison
42 ==, != equality, inequality
43 & bitwise AND
44 ^ bitwise XOR
45 | bitwise OR
46 && logical AND
47 || logical OR
48 expr ? expr : expr
b80f6443 49 conditional operator
ccc6cda3
JA
50 =, *=, /=, %=,
51 +=, -=, <<=, >>=,
52 &=, ^=, |= assignment
726f6388
JA
53
54Shell variables are allowed as operands. The name of the variable
7117c2d2 55is replaced by its value (coerced to a fixed-width integer) within
726f6388
JA
56an expression. The variable need not have its integer attribute
57turned on to be used in an expression.
58
59Operators are evaluated in order of precedence. Sub-expressions in
60parentheses are evaluated first and may override the precedence
61rules above.
62
3185942a 63Exit Status:
495aee44 64If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.
726f6388
JA
65$END
66
ccc6cda3
JA
67#include <config.h>
68
69#if defined (HAVE_UNISTD_H)
cce855bc
JA
70# ifdef _MINIX
71# include <sys/types.h>
72# endif
ccc6cda3
JA
73# include <unistd.h>
74#endif
75
b80f6443
JA
76#include "../bashintl.h"
77
726f6388 78#include "../shell.h"
ccc6cda3 79#include "common.h"
726f6388
JA
80
81/* Arithmetic LET function. */
ccc6cda3 82int
726f6388
JA
83let_builtin (list)
84 WORD_LIST *list;
85{
7117c2d2 86 intmax_t ret;
d166f048 87 int expok;
726f6388 88
7117c2d2
JA
89 /* Skip over leading `--' argument. */
90 if (list && list->word && ISOPTION (list->word->word, '-'))
91 list = list->next;
92
ccc6cda3 93 if (list == 0)
726f6388 94 {
b80f6443 95 builtin_error (_("expression expected"));
726f6388
JA
96 return (EXECUTION_FAILURE);
97 }
98
ccc6cda3 99 for (; list; list = list->next)
d166f048
JA
100 {
101 ret = evalexp (list->word->word, &expok);
102 if (expok == 0)
103 return (EXECUTION_FAILURE);
104 }
ccc6cda3 105
f73dda09 106 return ((ret == 0) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
ccc6cda3
JA
107}
108
d166f048 109#ifdef INCLUDE_UNUSED
ccc6cda3
JA
110int
111exp_builtin (list)
112 WORD_LIST *list;
113{
114 char *exp;
7117c2d2
JA
115 intmax_t ret;
116 int expok;
ccc6cda3
JA
117
118 if (list == 0)
726f6388 119 {
b80f6443 120 builtin_error (_("expression expected"));
ccc6cda3 121 return (EXECUTION_FAILURE);
726f6388
JA
122 }
123
ccc6cda3 124 exp = string_list (list);
d166f048
JA
125 ret = evalexp (exp, &expok);
126 (void)free (exp);
f73dda09 127 return (((ret == 0) || (expok == 0)) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
726f6388 128}
d166f048 129#endif