blob: 593a0ea91e15cdce503c98c3fb8ee8e56e48c15b [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>.
Massimiliano Minella847f69c2024-02-08 15:58:27 +010042 If no substring matching <r> is found in <t>, assign <t> to <name>.
Roland Gaudigd5ae0ec2021-07-23 12:29:22 +000043
44setexpr name sub <r> <s> [<t>]
45 Just like gsub(), but replace only the first matching substring
46
47The setexpr command takes the following arguments:
48
49format
50 This parameter contains a C or Bash like format string.
51 The number of arguments is limited to 4.
52 The following format types are supported:
53
54 c
55 single character
56 d, i
57 decimal value
58 o
59 octal value
60 s
61 string
62 u
63 unsigned decimal value
64 x, X
65 hexadecimal value
66 '%'
67 no conversion, instead a % character will be written
68
69 Backslash escapes:
70
71 \" = double quote
72 \\ = backslash
73 \a = alert (bell)
74 \b = backspace
75 \c = produce no further output
76 \f = form feed
77 \n = new line
78 \r = carriage return
79 \t = horizontal tab
80 \v = vertical tab
81 \NNN = octal number (NNN is 0 to 3 digits)
82
83name
84 The name of the environment variable to be set
85
86op
87 '|'
88 name = value | value2
89 '&'
90 name = value & value2
91 '+'
92 name = value + value2
93 (This is the only operator supported for strings.
94 It acts as concatenation operator on strings)
95 '^'
96 name = value ^ value2
97 '-'
98 name = value - value2
99 '*'
100 name = value * value2
101 '/'
102 name = value / value2
103 '%'
104 name = value % value2
105
106r
107 Regular expression
108
109s
110 Substitution string
111
112t
113 string
114
115value
116 Can either be an integer value, a string.
117 If the pointer prefix '*' is given value is treated as memory address.
118
119value2
120 See value
121
122Example
123-------
124
125::
126
127 => setexpr foo fmt %d 0x100
128 => echo $foo
129 256
130 =>
131
132 => setexpr foo fmt 0x%08x 63
133 => echo $foo
134 0x00000063
135 =>
136
137 => setexpr foo fmt %%%o 8
138 => echo $foo
139 %10
140 =>
141
142Configuration
143-------------
144
Heinrich Schuchardtdfeffbc2023-01-27 22:00:30 +0100145* The *setexpr* command is only available if CMD_SETEXPR=y.
146* The *setexpr fmt* sub-command is only available if CMD_SETEXPR_FMT=y.
147* The *setexpr gsub* and *setexpr sub* sub-commands are only available if
148 CONFIG_REGEX=y.
Roland Gaudigd5ae0ec2021-07-23 12:29:22 +0000149
150Return value
151------------
152
153The return value $? is set to 0 (true) if the operation was successful.
154
155If an error occurs, the return value $? is set to 1 (false).