1 |
#!/bin/bash |
2 |
# |
3 |
# echo, read, mapfile |
4 |
# TODO mapfile options: -c, -C, -u, etc. |
5 |
|
6 |
#### echo dashes |
7 |
echo - |
8 |
echo -- |
9 |
echo --- |
10 |
## stdout-json: "-\n--\n---\n" |
11 |
## BUG zsh stdout-json: "\n--\n---\n" |
12 |
|
13 |
#### echo backslashes |
14 |
echo \\ |
15 |
echo '\' |
16 |
echo '\\' |
17 |
echo "\\" |
18 |
## STDOUT: |
19 |
\ |
20 |
\ |
21 |
\\ |
22 |
\ |
23 |
## BUG dash/mksh/zsh STDOUT: |
24 |
\ |
25 |
\ |
26 |
\ |
27 |
\ |
28 |
## END |
29 |
|
30 |
#### echo -e backslashes |
31 |
echo -e \\ |
32 |
echo -e '\' |
33 |
echo -e '\\' |
34 |
echo -e "\\" |
35 |
## STDOUT: |
36 |
\ |
37 |
\ |
38 |
\ |
39 |
\ |
40 |
## N-I dash STDOUT: |
41 |
-e \ |
42 |
-e \ |
43 |
-e \ |
44 |
-e \ |
45 |
## END |
46 |
|
47 |
#### echo -en |
48 |
echo -en 'abc\ndef\n' |
49 |
## stdout-json: "abc\ndef\n" |
50 |
## N-I dash stdout-json: "-en abc\ndef\n\n" |
51 |
|
52 |
#### echo -ez (invalid flag) |
53 |
# bash differs from the other three shells, but its behavior is possibly more |
54 |
# sensible, if you're going to ignore the error. It doesn't make sense for |
55 |
# the 'e' to mean 2 different things simultaneously: flag and literal to be |
56 |
# printed. |
57 |
echo -ez 'abc\n' |
58 |
## stdout-json: "-ez abc\\n\n" |
59 |
## OK dash/mksh/zsh stdout-json: "-ez abc\n\n" |
60 |
|
61 |
#### echo -e with embedded newline |
62 |
flags='-e' |
63 |
case $SH in dash) flags='' ;; esac |
64 |
|
65 |
echo $flags 'foo |
66 |
bar' |
67 |
## STDOUT: |
68 |
foo |
69 |
bar |
70 |
## END |
71 |
|
72 |
#### echo -e line continuation |
73 |
flags='-e' |
74 |
case $SH in dash) flags='' ;; esac |
75 |
|
76 |
echo $flags 'foo\ |
77 |
bar' |
78 |
## STDOUT: |
79 |
foo\ |
80 |
bar |
81 |
## END |
82 |
|
83 |
#### echo -e with C escapes |
84 |
# https://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins |
85 |
# not sure why \c is like NUL? |
86 |
# zsh doesn't allow \E for some reason. |
87 |
echo -e '\a\b\d\e\f' |
88 |
## stdout-json: "\u0007\u0008\\d\u001b\u000c\n" |
89 |
## N-I dash stdout-json: "-e \u0007\u0008\\d\\e\u000c\n" |
90 |
|
91 |
#### echo -e with whitespace C escapes |
92 |
echo -e '\n\r\t\v' |
93 |
## stdout-json: "\n\r\t\u000b\n" |
94 |
## N-I dash stdout-json: "-e \n\r\t\u000b\n" |
95 |
|
96 |
#### \0 |
97 |
echo -e 'ab\0cd' |
98 |
## stdout-json: "ab\u0000cd\n" |
99 |
## N-I dash stdout-json: "-e ab\u0000cd\n" |
100 |
|
101 |
#### \c stops processing input |
102 |
flags='-e' |
103 |
case $SH in dash) flags='' ;; esac |
104 |
|
105 |
echo $flags xy 'ab\cde' 'zzz' |
106 |
## stdout-json: "xy ab" |
107 |
## N-I mksh stdout-json: "xy abde zzz" |
108 |
|
109 |
#### echo -e with hex escape |
110 |
echo -e 'abcd\x65f' |
111 |
## stdout-json: "abcdef\n" |
112 |
## N-I dash stdout-json: "-e abcd\\x65f\n" |
113 |
|
114 |
#### echo -e with octal escape |
115 |
flags='-e' |
116 |
case $SH in dash) flags='' ;; esac |
117 |
|
118 |
echo $flags 'abcd\044e' |
119 |
## stdout-json: "abcd$e\n" |
120 |
|
121 |
#### echo -e with 4 digit unicode escape |
122 |
flags='-e' |
123 |
case $SH in dash) flags='' ;; esac |
124 |
|
125 |
echo $flags 'abcd\u0065f' |
126 |
## STDOUT: |
127 |
abcdef |
128 |
## END |
129 |
## N-I dash/ash stdout-json: "abcd\\u0065f\n" |
130 |
|
131 |
#### echo -e with 8 digit unicode escape |
132 |
flags='-e' |
133 |
case $SH in dash) flags='' ;; esac |
134 |
|
135 |
echo $flags 'abcd\U00000065f' |
136 |
## STDOUT: |
137 |
abcdef |
138 |
## END |
139 |
## N-I dash/ash stdout-json: "abcd\\U00000065f\n" |
140 |
|
141 |
#### \0377 is the highest octal byte |
142 |
echo -en '\03777' | od -A n -t x1 | sed 's/ \+/ /g' |
143 |
## stdout-json: " ff 37\n" |
144 |
## N-I dash stdout-json: " 2d 65 6e 20 ff 37 0a\n" |
145 |
|
146 |
#### \0400 is one more than the highest octal byte |
147 |
# It is 256 % 256 which gets interpreted as a NUL byte. |
148 |
echo -en '\04000' | od -A n -t x1 | sed 's/ \+/ /g' |
149 |
## stdout-json: " 00 30\n" |
150 |
## BUG ash stdout-json: " 20 30 30\n" |
151 |
## N-I dash stdout-json: " 2d 65 6e 20 00 30 0a\n" |
152 |
|
153 |
#### \0777 is out of range |
154 |
flags='-en' |
155 |
case $SH in dash) flags='-n' ;; esac |
156 |
|
157 |
echo $flags '\0777' | od -A n -t x1 | sed 's/ \+/ /g' |
158 |
## stdout-json: " ff\n" |
159 |
## BUG mksh stdout-json: " c3 bf\n" |
160 |
## BUG ash stdout-json: " 3f 37\n" |
161 |
|
162 |
#### incomplete hex escape |
163 |
echo -en 'abcd\x6' | od -A n -c | sed 's/ \+/ /g' |
164 |
## stdout-json: " a b c d 006\n" |
165 |
## N-I dash stdout-json: " - e n a b c d \\ x 6 \\n\n" |
166 |
|
167 |
#### \x |
168 |
# I consider mksh and zsh a bug because \x is not an escape |
169 |
echo -e '\x' '\xg' | od -A n -c | sed 's/ \+/ /g' |
170 |
## stdout-json: " \\ x \\ x g \\n\n" |
171 |
## N-I dash stdout-json: " - e \\ x \\ x g \\n\n" |
172 |
## BUG mksh/zsh stdout-json: " \\0 \\0 g \\n\n" |
173 |
|
174 |
#### incomplete octal escape |
175 |
flags='-en' |
176 |
case $SH in dash) flags='-n' ;; esac |
177 |
|
178 |
echo $flags 'abcd\04' | od -A n -c | sed 's/ \+/ /g' |
179 |
## stdout-json: " a b c d 004\n" |
180 |
|
181 |
#### incomplete unicode escape |
182 |
echo -en 'abcd\u006' | od -A n -c | sed 's/ \+/ /g' |
183 |
## stdout-json: " a b c d 006\n" |
184 |
## N-I dash stdout-json: " - e n a b c d \\ u 0 0 6 \\n\n" |
185 |
## BUG ash stdout-json: " a b c d \\ u 0 0 6\n" |
186 |
|
187 |
#### \u6 |
188 |
flags='-en' |
189 |
case $SH in dash) flags='-n' ;; esac |
190 |
|
191 |
echo $flags '\u6' | od -A n -c | sed 's/ \+/ /g' |
192 |
## stdout-json: " 006\n" |
193 |
## N-I dash/ash stdout-json: " \\ u 6\n" |
194 |
|
195 |
#### \0 \1 \8 |
196 |
# \0 is special, but \1 isn't in bash |
197 |
# \1 is special in dash! geez |
198 |
flags='-en' |
199 |
case $SH in dash) flags='-n' ;; esac |
200 |
|
201 |
echo $flags '\0' '\1' '\8' | od -A n -c | sed 's/ \+/ /g' |
202 |
## stdout-json: " \\0 \\ 1 \\ 8\n" |
203 |
## BUG dash/ash stdout-json: " \\0 001 \\ 8\n" |
204 |
|
205 |
#### Read builtin |
206 |
# NOTE: there are TABS below |
207 |
read x <<EOF |
208 |
A B C D E |
209 |
FG |
210 |
EOF |
211 |
echo "[$x]" |
212 |
## stdout: [A B C D E] |
213 |
## status: 0 |
214 |
|
215 |
#### Read from empty file |
216 |
echo -n '' > $TMP/empty.txt |
217 |
read x < $TMP/empty.txt |
218 |
argv.py "status=$?" "$x" |
219 |
|
220 |
# No variable name, behaves the same |
221 |
read < $TMP/empty.txt |
222 |
argv.py "status=$?" "$REPLY" |
223 |
|
224 |
## STDOUT: |
225 |
['status=1', ''] |
226 |
['status=1', ''] |
227 |
## END |
228 |
## OK dash STDOUT: |
229 |
['status=1', ''] |
230 |
['status=2', ''] |
231 |
## END |
232 |
## status: 0 |
233 |
|
234 |
#### read /dev/null |
235 |
read -n 1 </dev/null |
236 |
echo $? |
237 |
## STDOUT: |
238 |
1 |
239 |
## END |
240 |
## OK dash stdout: 2 |
241 |
|
242 |
|
243 |
#### read with zero args |
244 |
echo | read |
245 |
echo status=$? |
246 |
## STDOUT: |
247 |
status=0 |
248 |
## END |
249 |
## BUG dash STDOUT: |
250 |
status=2 |
251 |
## END |
252 |
|
253 |
#### Read builtin with no newline. |
254 |
# This is odd because the variable is populated successfully. OSH/Oil might |
255 |
# need a separate put reading feature that doesn't use IFS. |
256 |
echo -n ZZZ | { read x; echo $?; echo $x; } |
257 |
## stdout-json: "1\nZZZ\n" |
258 |
## status: 0 |
259 |
|
260 |
#### Read builtin with multiple variables |
261 |
# NOTE: there are TABS below |
262 |
read x y z <<EOF |
263 |
A B C D E |
264 |
FG |
265 |
EOF |
266 |
echo "[$x/$y/$z]" |
267 |
## stdout: [A/B/C D E] |
268 |
## status: 0 |
269 |
|
270 |
#### Read builtin with not enough variables |
271 |
set -o errexit |
272 |
set -o nounset # hm this doesn't change it |
273 |
read x y z <<EOF |
274 |
A B |
275 |
EOF |
276 |
echo /$x/$y/$z/ |
277 |
## stdout: /A/B// |
278 |
## status: 0 |
279 |
|
280 |
#### Read -n (with $REPLY) |
281 |
echo 12345 > $TMP/readn.txt |
282 |
read -n 4 x < $TMP/readn.txt |
283 |
read -n 2 < $TMP/readn.txt # Do it again with no variable |
284 |
argv.py $x $REPLY |
285 |
## stdout: ['1234', '12'] |
286 |
## N-I dash/zsh stdout: [] |
287 |
|
288 |
#### IFS= read -n (OSH regression: value saved in tempenv) |
289 |
echo XYZ > "$TMP/readn.txt" |
290 |
IFS= TMOUT= read -n 1 char < "$TMP/readn.txt" |
291 |
argv.py "$char" |
292 |
## stdout: ['X'] |
293 |
## N-I dash/zsh stdout: [''] |
294 |
|
295 |
#### Read uses $REPLY (without -n) |
296 |
echo 123 > $TMP/readreply.txt |
297 |
read < $TMP/readreply.txt |
298 |
echo $REPLY |
299 |
## stdout: 123 |
300 |
## N-I dash stdout: |
301 |
|
302 |
#### read -r ignores backslashes |
303 |
echo 'one\ two' > $TMP/readr.txt |
304 |
read escaped < $TMP/readr.txt |
305 |
read -r raw < $TMP/readr.txt |
306 |
argv.py "$escaped" "$raw" |
307 |
## stdout: ['one two', 'one\\ two'] |
308 |
|
309 |
#### read -r with other backslash escapes |
310 |
echo 'one\ two\x65three' > $TMP/readr.txt |
311 |
read escaped < $TMP/readr.txt |
312 |
read -r raw < $TMP/readr.txt |
313 |
argv.py "$escaped" "$raw" |
314 |
# mksh respects the hex escapes here, but other shells don't! |
315 |
## stdout: ['one twox65three', 'one\\ two\\x65three'] |
316 |
## BUG mksh/zsh stdout: ['one twoethree', 'one\\ twoethree'] |
317 |
|
318 |
#### read with line continuation reads multiple physical lines |
319 |
# NOTE: osh failing because of file descriptor issue. stdin has to be closed! |
320 |
tmp=$TMP/$(basename $SH)-readr.txt |
321 |
echo -e 'one\\\ntwo\n' > $tmp |
322 |
read escaped < $tmp |
323 |
read -r raw < $tmp |
324 |
argv.py "$escaped" "$raw" |
325 |
## stdout: ['onetwo', 'one\\'] |
326 |
## N-I dash stdout: ['-e onetwo', '-e one\\'] |
327 |
|
328 |
#### read multiple vars spanning many lines |
329 |
read x y << 'EOF' |
330 |
one-\ |
331 |
two three-\ |
332 |
four five-\ |
333 |
six |
334 |
EOF |
335 |
argv.py "$x" "$y" "$z" |
336 |
## stdout: ['one-two', 'three-four five-six', ''] |
337 |
|
338 |
#### read -r with \n |
339 |
echo '\nline' > $TMP/readr.txt |
340 |
read escaped < $TMP/readr.txt |
341 |
read -r raw < $TMP/readr.txt |
342 |
argv.py "$escaped" "$raw" |
343 |
# dash/mksh/zsh are bugs because at least the raw mode should let you read a |
344 |
# literal \n. |
345 |
## stdout: ['nline', '\\nline'] |
346 |
## BUG dash/mksh/zsh stdout: ['', ''] |
347 |
|
348 |
#### Read with IFS=$'\n' |
349 |
# The leading spaces are stripped if they appear in IFS. |
350 |
IFS=$(echo -e '\n') |
351 |
read var <<EOF |
352 |
a b c |
353 |
d e f |
354 |
EOF |
355 |
echo "[$var]" |
356 |
## stdout: [ a b c] |
357 |
## N-I dash stdout: [a b c] |
358 |
|
359 |
#### Read multiple lines with IFS=: |
360 |
# The leading spaces are stripped if they appear in IFS. |
361 |
# IFS chars are escaped with :. |
362 |
tmp=$TMP/$(basename $SH)-read-ifs.txt |
363 |
IFS=: |
364 |
cat >$tmp <<'EOF' |
365 |
\\a :b\: c:d\ |
366 |
e |
367 |
EOF |
368 |
read a b c d < $tmp |
369 |
# Use printf because echo in dash/mksh interprets escapes, while it doesn't in |
370 |
# bash. |
371 |
printf "%s\n" "[$a|$b|$c|$d]" |
372 |
## stdout: [ \a |b: c|d e|] |
373 |
|
374 |
#### Read with IFS='' |
375 |
IFS='' |
376 |
read x y <<EOF |
377 |
a b c d |
378 |
EOF |
379 |
echo "[$x|$y]" |
380 |
## stdout: [ a b c d|] |
381 |
|
382 |
#### Read should not respect C escapes. |
383 |
# bash doesn't respect these, but other shells do. Gah! I think bash |
384 |
# behavior makes more sense. It only escapes IFS. |
385 |
echo '\a \b \c \d \e \f \g \h \x65 \145 \i' > $TMP/read-c.txt |
386 |
read line < $TMP/read-c.txt |
387 |
echo $line |
388 |
## stdout-json: "a b c d e f g h x65 145 i\n" |
389 |
## BUG ash stdout-json: "abcdefghx65 145 i\n" |
390 |
## BUG dash/zsh stdout-json: "\u0007 \u0008\n" |
391 |
## BUG mksh stdout-json: "\u0007 \u0008 d \u001b \u000c g h e 145 i\n" |
392 |
|
393 |
#### Read builtin uses dynamic scope |
394 |
f() { |
395 |
read head << EOF |
396 |
ref: refs/heads/dev/andy |
397 |
EOF |
398 |
} |
399 |
f |
400 |
echo $head |
401 |
## STDOUT: |
402 |
ref: refs/heads/dev/andy |
403 |
## END |
404 |
|
405 |
#### read -a reads into array |
406 |
|
407 |
# read -a is used in bash-completion |
408 |
# none of these shells implement it |
409 |
case $SH in |
410 |
*mksh|*dash|*zsh|*/ash) |
411 |
exit 2; |
412 |
;; |
413 |
esac |
414 |
|
415 |
read -a myarray <<'EOF' |
416 |
a b c\ d |
417 |
EOF |
418 |
argv.py "${myarray[@]}" |
419 |
|
420 |
# arguments are ignored here |
421 |
read -r -a array2 extra arguments <<'EOF' |
422 |
a b c\ d |
423 |
EOF |
424 |
argv.py "${array2[@]}" |
425 |
argv.py "${extra[@]}" |
426 |
argv.py "${arguments[@]}" |
427 |
## status: 0 |
428 |
## STDOUT: |
429 |
['a', 'b', 'c d'] |
430 |
['a', 'b', 'c\\', 'd'] |
431 |
[] |
432 |
[] |
433 |
## END |
434 |
## N-I dash/mksh/zsh/ash status: 2 |
435 |
## N-I dash/mksh/zsh/ash stdout-json: "" |
436 |
|
437 |
#### read -n with invalid arg |
438 |
read -n not_a_number |
439 |
echo status=$? |
440 |
## stdout: status=2 |
441 |
## OK bash stdout: status=1 |
442 |
## N-I zsh stdout-json: "" |
443 |
|
444 |
#### read returns correct number of bytes without EOF |
445 |
case $SH in |
446 |
*bash|*osh) FLAG=n ;; |
447 |
*mksh) FLAG=N ;; |
448 |
*) exit ;; # other shells don't implement it, or hang |
449 |
esac |
450 |
|
451 |
i=0 |
452 |
while true; do |
453 |
echo -n x |
454 |
|
455 |
(( i++ )) |
456 |
|
457 |
# TODO: Why does OSH hang without this test? Other shells are fine. I can't |
458 |
# reproduce outside of sh_spec.py. |
459 |
if test $i = 100; then |
460 |
break |
461 |
#true |
462 |
fi |
463 |
done | { read -$FLAG 3; echo $REPLY; } |
464 |
|
465 |
## status: 0 |
466 |
## stdout: xxx |
467 |
## N-I dash/ash stdout-json: "" |
468 |
|
469 |
# zsh appears to hang with -k |
470 |
## N-I zsh stdout-json: "" |
471 |
|
472 |
#### read -d : (colon-separated records) |
473 |
printf a,b,c:d,e,f:g,h,i | { |
474 |
IFS=, |
475 |
read -d : v1 |
476 |
echo "v1=$v1" |
477 |
read -d : v1 v2 |
478 |
echo "v1=$v1 v2=$v2" |
479 |
read -d : v1 v2 v3 |
480 |
echo "v1=$v1 v2=$v2 v3=$v3" |
481 |
} |
482 |
## STDOUT: |
483 |
v1=a,b,c |
484 |
v1=d v2=e,f |
485 |
v1=g v2=h v3=i |
486 |
## END |
487 |
## N-I dash STDOUT: |
488 |
v1= |
489 |
v1= v2= |
490 |
v1= v2= v3= |
491 |
## END |
492 |
## BUG ash STDOUT: |
493 |
v1=a,b,c |
494 |
v1=d,e,f v2= |
495 |
v1=g,h,i v2= v3= |
496 |
## END |
497 |
|
498 |
#### read -d '' (null-separated records) |
499 |
printf 'a,b,c\0d,e,f\0g,h,i' | { |
500 |
IFS=, |
501 |
read -d '' v1 |
502 |
echo "v1=$v1" |
503 |
read -d '' v1 v2 |
504 |
echo "v1=$v1 v2=$v2" |
505 |
read -d '' v1 v2 v3 |
506 |
echo "v1=$v1 v2=$v2 v3=$v3" |
507 |
} |
508 |
## STDOUT: |
509 |
v1=a,b,c |
510 |
v1=d v2=e,f |
511 |
v1=g v2=h v3=i |
512 |
## END |
513 |
## N-I dash STDOUT: |
514 |
v1= |
515 |
v1= v2= |
516 |
v1= v2= v3= |
517 |
## END |
518 |
## BUG ash STDOUT: |
519 |
v1=a,b,cd,e,fg,h,i |
520 |
v1= v2= |
521 |
v1= v2= v3= |
522 |
## END |
523 |
|
524 |
#### read -rd |
525 |
read -rd '' var <<EOF |
526 |
foo |
527 |
bar |
528 |
EOF |
529 |
echo "$var" |
530 |
## STDOUT: |
531 |
foo |
532 |
bar |
533 |
## END |
534 |
## N-I dash stdout-json: "\n" |
535 |
|
536 |
#### read -d when there's no delimiter |
537 |
{ read -d : part |
538 |
echo $part $? |
539 |
read -d : part |
540 |
echo $part $? |
541 |
} <<EOF |
542 |
foo:bar |
543 |
EOF |
544 |
## STDOUT: |
545 |
foo 0 |
546 |
bar 1 |
547 |
## END |
548 |
## N-I dash STDOUT: |
549 |
2 |
550 |
2 |
551 |
## END |
552 |
|
553 |
#### read usage |
554 |
read -n -1 |
555 |
echo status=$? |
556 |
## STDOUT: |
557 |
status=2 |
558 |
## END |
559 |
## OK bash stdout: status=1 |
560 |
## BUG mksh stdout-json: "" |
561 |
# zsh gives a fatal error? seems inconsistent |
562 |
## BUG zsh stdout-json: "" |
563 |
## BUG zsh status: 1 |
564 |
|
565 |
#### mapfile |
566 |
type mapfile >/dev/null 2>&1 || exit 0 |
567 |
printf '%s\n' {1..5..2} | { |
568 |
mapfile |
569 |
echo "n=${#MAPFILE[@]}" |
570 |
printf '[%s]\n' "${MAPFILE[@]}" |
571 |
} |
572 |
## STDOUT: |
573 |
n=3 |
574 |
[1 |
575 |
] |
576 |
[3 |
577 |
] |
578 |
[5 |
579 |
] |
580 |
## END |
581 |
## N-I dash/mksh/zsh/ash stdout-json: "" |
582 |
|
583 |
#### readarray (synonym for mapfile) |
584 |
type readarray >/dev/null 2>&1 || exit 0 |
585 |
printf '%s\n' {1..5..2} | { |
586 |
readarray |
587 |
echo "n=${#MAPFILE[@]}" |
588 |
printf '[%s]\n' "${MAPFILE[@]}" |
589 |
} |
590 |
## STDOUT: |
591 |
n=3 |
592 |
[1 |
593 |
] |
594 |
[3 |
595 |
] |
596 |
[5 |
597 |
] |
598 |
## END |
599 |
## N-I dash/mksh/zsh/ash stdout-json: "" |
600 |
|
601 |
#### mapfile (array name): arr |
602 |
type mapfile >/dev/null 2>&1 || exit 0 |
603 |
printf '%s\n' {1..5..2} | { |
604 |
mapfile arr |
605 |
echo "n=${#arr[@]}" |
606 |
printf '[%s]\n' "${arr[@]}" |
607 |
} |
608 |
## STDOUT: |
609 |
n=3 |
610 |
[1 |
611 |
] |
612 |
[3 |
613 |
] |
614 |
[5 |
615 |
] |
616 |
## END |
617 |
## N-I dash/mksh/zsh/ash stdout-json: "" |
618 |
|
619 |
#### mapfile (delimeter): -d delim |
620 |
# Note: Bash-4.4+ |
621 |
type mapfile >/dev/null 2>&1 || exit 0 |
622 |
printf '%s:' {1..5..2} | { |
623 |
mapfile -d : arr |
624 |
echo "n=${#arr[@]}" |
625 |
printf '[%s]\n' "${arr[@]}" |
626 |
} |
627 |
## STDOUT: |
628 |
n=3 |
629 |
[1:] |
630 |
[3:] |
631 |
[5:] |
632 |
## END |
633 |
## N-I dash/mksh/zsh/ash stdout-json: "" |
634 |
|
635 |
#### mapfile (delimiter): -d '' (null-separated) |
636 |
# Note: Bash-4.4+ |
637 |
type mapfile >/dev/null 2>&1 || exit 0 |
638 |
printf '%s\0' {1..5..2} | { |
639 |
mapfile -d '' arr |
640 |
echo "n=${#arr[@]}" |
641 |
printf '[%s]\n' "${arr[@]}" |
642 |
} |
643 |
## STDOUT: |
644 |
n=3 |
645 |
[1] |
646 |
[3] |
647 |
[5] |
648 |
## END |
649 |
## N-I dash/mksh/zsh/ash stdout-json: "" |
650 |
|
651 |
#### mapfile (truncate delim): -t |
652 |
type mapfile >/dev/null 2>&1 || exit 0 |
653 |
printf '%s\n' {1..5..2} | { |
654 |
mapfile -t arr |
655 |
echo "n=${#arr[@]}" |
656 |
printf '[%s]\n' "${arr[@]}" |
657 |
} |
658 |
## STDOUT: |
659 |
n=3 |
660 |
[1] |
661 |
[3] |
662 |
[5] |
663 |
## END |
664 |
## N-I dash/mksh/zsh/ash stdout-json: "" |
665 |
|
666 |
#### mapfile (store position): -O start |
667 |
type mapfile >/dev/null 2>&1 || exit 0 |
668 |
printf '%s\n' a{0..2} | { |
669 |
arr=(x y z) |
670 |
mapfile -O 2 -t arr |
671 |
echo "n=${#arr[@]}" |
672 |
printf '[%s]\n' "${arr[@]}" |
673 |
} |
674 |
## STDOUT: |
675 |
n=5 |
676 |
[x] |
677 |
[y] |
678 |
[a0] |
679 |
[a1] |
680 |
[a2] |
681 |
## END |
682 |
## N-I dash/mksh/zsh/ash stdout-json: "" |
683 |
|
684 |
#### mapfile (input range): -s start -n count |
685 |
type mapfile >/dev/null 2>&1 || exit 0 |
686 |
printf '%s\n' a{0..10} | { |
687 |
mapfile -s 5 -n 3 -t arr |
688 |
echo "n=${#arr[@]}" |
689 |
printf '[%s]\n' "${arr[@]}" |
690 |
} |
691 |
## STDOUT: |
692 |
n=3 |
693 |
[a5] |
694 |
[a6] |
695 |
[a7] |
696 |
## END |
697 |
## N-I dash/mksh/zsh/ash stdout-json: "" |