blob: d245a13ca88b5300ab83197679de052515af5941 [file] [log] [blame]
Roland Gaudigd5ae0ec2021-07-23 12:29:22 +00001.. SPDX-License-Identifier: GPL-2.0+
2
Heinrich Schuchardt1b0c3162024-01-14 14:53:13 +01003.. index::
4 single: setexpr (command)
5
Roland Gaudigd5ae0ec2021-07-23 12:29:22 +00006setexpr command
7===============
8
9Synopsis
10--------
11
12::
13
14 setexpr[.b, .w, .l .s] <name> [*]<value> <op> [*]<value2>
15 setexpr[.b, .w, .l] <name> [*]<value>
16 setexpr <name> fmt <format> [value]...
17 setexpr <name> gsub r s [t]
18 setexpr <name> sub r s [t]
19
20Description
21-----------
22
23The setexpr command is used to set an environment variable to the result
24of an evaluation.
25
26setexpr[.b, .w, .l .s] <name> [*]<value> <op> [*]<value2>
27 Set environment variable <name> to the result of the evaluated
28 expression specified by <op>.
29
30setexpr[.b, .w, .l] name [*]value
31 Load <value> into environment variable <name>
32
33setexpr name fmt <format> value
34 Set environment variable <name> to the result of the C like
35 format string <format> evaluation of <value>.
36
37setexpr name gsub <r> <s> [<t>]
38 For each substring matching the regular expression <r> in the
39 string <t>, substitute the string <s>.
40 The result is assigned to <name>.
41 If <t> is not supplied, use the old value of <name>.
42
43setexpr name sub <r> <s> [<t>]
44 Just like gsub(), but replace only the first matching substring
45
46The setexpr command takes the following arguments:
47
48format
49 This parameter contains a C or Bash like format string.
50 The number of arguments is limited to 4.
51 The following format types are supported:
52
53 c
54 single character
55 d, i
56 decimal value
57 o
58 octal value
59 s
60 string
61 u
62 unsigned decimal value
63 x, X
64 hexadecimal value
65 '%'
66 no conversion, instead a % character will be written
67
68 Backslash escapes:
69
70 \" = double quote
71 \\ = backslash
72 \a = alert (bell)
73 \b = backspace
74 \c = produce no further output
75 \f = form feed
76 \n = new line
77 \r = carriage return
78 \t = horizontal tab
79 \v = vertical tab
80 \NNN = octal number (NNN is 0 to 3 digits)
81
82name
83 The name of the environment variable to be set
84
85op
86 '|'
87 name = value | value2
88 '&'
89 name = value & value2
90 '+'
91 name = value + value2
92 (This is the only operator supported for strings.
93 It acts as concatenation operator on strings)
94 '^'
95 name = value ^ value2
96 '-'
97 name = value - value2
98 '*'
99 name = value * value2
100 '/'
101 name = value / value2
102 '%'
103 name = value % value2
104
105r
106 Regular expression
107
108s
109 Substitution string
110
111t
112 string
113
114value
115 Can either be an integer value, a string.
116 If the pointer prefix '*' is given value is treated as memory address.
117
118value2
119 See value
120
121Example
122-------
123
124::
125
126 => setexpr foo fmt %d 0x100
127 => echo $foo
128 256
129 =>
130
131 => setexpr foo fmt 0x%08x 63
132 => echo $foo
133 0x00000063
134 =>
135
136 => setexpr foo fmt %%%o 8
137 => echo $foo
138 %10
139 =>
140
141Configuration
142-------------
143
Heinrich Schuchardtdfeffbc2023-01-27 22:00:30 +0100144* The *setexpr* command is only available if CMD_SETEXPR=y.
145* The *setexpr fmt* sub-command is only available if CMD_SETEXPR_FMT=y.
146* The *setexpr gsub* and *setexpr sub* sub-commands are only available if
147 CONFIG_REGEX=y.
Roland Gaudigd5ae0ec2021-07-23 12:29:22 +0000148
149Return value
150------------
151
152The return value $? is set to 0 (true) if the operation was successful.
153
154If an error occurs, the return value $? is set to 1 (false).