| 1 |
## compare_shells: bash zsh-5.1.1 zsh-5.9 mksh dash ash yash
|
| 2 |
## oils_failures_allowed: 0
|
| 3 |
|
| 4 |
# NOTE:
|
| 5 |
# This entire file was generated using gemini-3
|
| 6 |
|
| 7 |
# ==============================================================================
|
| 8 |
# SECTION 3.1: SHELL SYNTAX
|
| 9 |
# ==============================================================================
|
| 10 |
|
| 11 |
|
| 12 |
echo 'single quotes preserve $variables and \backslashes'
|
| 13 |
## STDOUT:
|
| 14 |
single quotes preserve $variables and \backslashes
|
| 15 |
## END
|
| 16 |
|
| 17 |
|
| 18 |
foo="bar"
|
| 19 |
echo "double quotes expand \$foo: $foo"
|
| 20 |
## STDOUT:
|
| 21 |
double quotes expand $foo: bar
|
| 22 |
## END
|
| 23 |
|
| 24 |
|
| 25 |
# Test widely supported escapes: newline, tab, hex
|
| 26 |
echo $'Line1\nLine2'
|
| 27 |
echo $'Tab\tCharacter'
|
| 28 |
echo $'Hex\x41'
|
| 29 |
## STDOUT:
|
| 30 |
Line1
|
| 31 |
Line2
|
| 32 |
Tab Character
|
| 33 |
HexA
|
| 34 |
## END
|
| 35 |
|
| 36 |
|
| 37 |
# Verify basic unicode code point handling
|
| 38 |
echo $'\u263a'
|
| 39 |
## STDOUT:
|
| 40 |
☺
|
| 41 |
## END
|
| 42 |
|
| 43 |
# ==============================================================================
|
| 44 |
# SECTION 3.2: SHELL COMMANDS
|
| 45 |
# ==============================================================================
|
| 46 |
|
| 47 |
|
| 48 |
echo "pipeline test" | cat
|
| 49 |
## STDOUT:
|
| 50 |
pipeline test
|
| 51 |
## END
|
| 52 |
|
| 53 |
|
| 54 |
! true
|
| 55 |
echo status=$?
|
| 56 |
! false
|
| 57 |
echo status=$?
|
| 58 |
## STDOUT:
|
| 59 |
status=1
|
| 60 |
status=0
|
| 61 |
## END
|
| 62 |
|
| 63 |
|
| 64 |
set +o pipefail
|
| 65 |
true | false | true
|
| 66 |
echo "pipefail off: $?"
|
| 67 |
set -o pipefail
|
| 68 |
true | false | true
|
| 69 |
echo "pipefail on: $?"
|
| 70 |
## STDOUT:
|
| 71 |
pipefail off: 0
|
| 72 |
pipefail on: 1
|
| 73 |
## END
|
| 74 |
|
| 75 |
|
| 76 |
true && echo "first" && echo "second"
|
| 77 |
false && echo "skipped"
|
| 78 |
echo "end"
|
| 79 |
## STDOUT:
|
| 80 |
first
|
| 81 |
second
|
| 82 |
end
|
| 83 |
## END
|
| 84 |
|
| 85 |
|
| 86 |
false || echo "recovered"
|
| 87 |
true || echo "skipped"
|
| 88 |
echo "end"
|
| 89 |
## STDOUT:
|
| 90 |
recovered
|
| 91 |
end
|
| 92 |
## END
|
| 93 |
|
| 94 |
# ==============================================================================
|
| 95 |
# SECTION 3.2.5: COMPOUND COMMANDS
|
| 96 |
# ==============================================================================
|
| 97 |
|
| 98 |
|
| 99 |
for i in 1 2 3; do
|
| 100 |
echo "num $i"
|
| 101 |
done
|
| 102 |
## STDOUT:
|
| 103 |
num 1
|
| 104 |
num 2
|
| 105 |
num 3
|
| 106 |
## END
|
| 107 |
|
| 108 |
|
| 109 |
i=3
|
| 110 |
while [ $i -gt 0 ]; do
|
| 111 |
echo "count $i"
|
| 112 |
i=$((i-1))
|
| 113 |
done
|
| 114 |
## STDOUT:
|
| 115 |
count 3
|
| 116 |
count 2
|
| 117 |
count 1
|
| 118 |
## END
|
| 119 |
|
| 120 |
|
| 121 |
for i in 1 2 3 4 5; do
|
| 122 |
if [ "$i" -eq 3 ]; then
|
| 123 |
break
|
| 124 |
fi
|
| 125 |
echo "$i"
|
| 126 |
done
|
| 127 |
## STDOUT:
|
| 128 |
1
|
| 129 |
2
|
| 130 |
## END
|
| 131 |
|
| 132 |
|
| 133 |
x=10
|
| 134 |
if [ "$x" -lt 5 ]; then
|
| 135 |
echo "less"
|
| 136 |
elif [ "$x" -eq 10 ]; then
|
| 137 |
echo "equal"
|
| 138 |
else
|
| 139 |
echo "greater"
|
| 140 |
fi
|
| 141 |
## STDOUT:
|
| 142 |
equal
|
| 143 |
## END
|
| 144 |
|
| 145 |
|
| 146 |
# Tests basic glob patterns in case
|
| 147 |
x="file.txt"
|
| 148 |
case "$x" in
|
| 149 |
*.sh) echo "script" ;;
|
| 150 |
*.txt) echo "text" ;;
|
| 151 |
*) echo "other" ;;
|
| 152 |
esac
|
| 153 |
## STDOUT:
|
| 154 |
text
|
| 155 |
## END
|
| 156 |
|
| 157 |
|
| 158 |
# Subshells should not affect parent environment
|
| 159 |
x=1
|
| 160 |
(
|
| 161 |
x=2
|
| 162 |
echo "inner: $x"
|
| 163 |
)
|
| 164 |
echo "outer: $x"
|
| 165 |
## STDOUT:
|
| 166 |
inner: 2
|
| 167 |
outer: 1
|
| 168 |
## END
|
| 169 |
|
| 170 |
|
| 171 |
# Braces run in current shell
|
| 172 |
x=1
|
| 173 |
{
|
| 174 |
x=2
|
| 175 |
echo "inner: $x"
|
| 176 |
}
|
| 177 |
echo "outer: $x"
|
| 178 |
## STDOUT:
|
| 179 |
inner: 2
|
| 180 |
outer: 2
|
| 181 |
## END
|
| 182 |
|
| 183 |
# ==============================================================================
|
| 184 |
# SECTION 3.4: SHELL PARAMETERS
|
| 185 |
# ==============================================================================
|
| 186 |
|
| 187 |
|
| 188 |
set -- a b c
|
| 189 |
echo "$1"
|
| 190 |
echo "$2"
|
| 191 |
echo "$3"
|
| 192 |
echo "$#"
|
| 193 |
## STDOUT:
|
| 194 |
a
|
| 195 |
b
|
| 196 |
c
|
| 197 |
3
|
| 198 |
## END
|
| 199 |
|
| 200 |
|
| 201 |
set -- "one two" three
|
| 202 |
for arg in "$@"; do
|
| 203 |
echo "arg: $arg"
|
| 204 |
done
|
| 205 |
echo "---"
|
| 206 |
for arg in "$*"; do
|
| 207 |
echo "arg: $arg"
|
| 208 |
done
|
| 209 |
## STDOUT:
|
| 210 |
arg: one two
|
| 211 |
arg: three
|
| 212 |
---
|
| 213 |
arg: one two three
|
| 214 |
## END
|
| 215 |
|
| 216 |
|
| 217 |
true
|
| 218 |
echo "t: $?"
|
| 219 |
false
|
| 220 |
echo "f: $?"
|
| 221 |
## STDOUT:
|
| 222 |
t: 0
|
| 223 |
f: 1
|
| 224 |
## END
|
| 225 |
|
| 226 |
# ==============================================================================
|
| 227 |
# SECTION 3.5: SHELL EXPANSIONS
|
| 228 |
# ==============================================================================
|
| 229 |
|
| 230 |
|
| 231 |
echo a{b,c,d}e
|
| 232 |
echo {1..3}
|
| 233 |
## STDOUT:
|
| 234 |
abe ace ade
|
| 235 |
1 2 3
|
| 236 |
## END
|
| 237 |
|
| 238 |
|
| 239 |
echo a{1,2{x,y}}b
|
| 240 |
## STDOUT:
|
| 241 |
a1b a2xb a2yb
|
| 242 |
## END
|
| 243 |
|
| 244 |
|
| 245 |
unset unset_var
|
| 246 |
empty_var=""
|
| 247 |
echo "1: ${unset_var:-default}"
|
| 248 |
echo "2: ${empty_var:-default}"
|
| 249 |
echo "3: ${unset_var:=assigned}"
|
| 250 |
echo "4: $unset_var"
|
| 251 |
## STDOUT:
|
| 252 |
1: default
|
| 253 |
2: default
|
| 254 |
3: assigned
|
| 255 |
4: assigned
|
| 256 |
## END
|
| 257 |
|
| 258 |
|
| 259 |
str="hello"
|
| 260 |
echo ${#str}
|
| 261 |
## STDOUT:
|
| 262 |
5
|
| 263 |
## END
|
| 264 |
|
| 265 |
|
| 266 |
# % is remove suffix (shortest), %% is remove suffix (longest)
|
| 267 |
path="/path/to/file.txt"
|
| 268 |
echo ${path%.*}
|
| 269 |
echo ${path##*/}
|
| 270 |
## STDOUT:
|
| 271 |
/path/to/file
|
| 272 |
file.txt
|
| 273 |
## END
|
| 274 |
|
| 275 |
|
| 276 |
echo "The date is $(echo today)"
|
| 277 |
echo "Backticks `echo work`"
|
| 278 |
## STDOUT:
|
| 279 |
The date is today
|
| 280 |
Backticks work
|
| 281 |
## END
|
| 282 |
|
| 283 |
|
| 284 |
echo $(( 1 + 2 * 3 ))
|
| 285 |
x=5
|
| 286 |
echo $(( x += 5 ))
|
| 287 |
## STDOUT:
|
| 288 |
7
|
| 289 |
10
|
| 290 |
## END
|
| 291 |
|
| 292 |
|
| 293 |
# Note: process substitution is not POSIX, but Bash/OSH support it.
|
| 294 |
cat <(echo "inside")
|
| 295 |
## STDOUT:
|
| 296 |
inside
|
| 297 |
## END
|
| 298 |
|
| 299 |
# ==============================================================================
|
| 300 |
# SECTION 3.6: REDIRECTIONS
|
| 301 |
# ==============================================================================
|
| 302 |
|
| 303 |
|
| 304 |
echo "content" > test_out.txt
|
| 305 |
cat test_out.txt
|
| 306 |
rm test_out.txt
|
| 307 |
## STDOUT:
|
| 308 |
content
|
| 309 |
## END
|
| 310 |
|
| 311 |
|
| 312 |
echo "line1" > test_append.txt
|
| 313 |
echo "line2" >> test_append.txt
|
| 314 |
cat test_append.txt
|
| 315 |
rm test_append.txt
|
| 316 |
## STDOUT:
|
| 317 |
line1
|
| 318 |
line2
|
| 319 |
## END
|
| 320 |
|
| 321 |
|
| 322 |
# Redirect stdout to stderr
|
| 323 |
{ echo "to stderr" >&2; } 2>&1
|
| 324 |
## STDOUT:
|
| 325 |
to stderr
|
| 326 |
## END
|
| 327 |
|
| 328 |
|
| 329 |
cat <<EOF
|
| 330 |
line 1
|
| 331 |
line 2
|
| 332 |
EOF
|
| 333 |
## STDOUT:
|
| 334 |
line 1
|
| 335 |
line 2
|
| 336 |
## END
|
| 337 |
|
| 338 |
|
| 339 |
grep "b" <<< "abc"
|
| 340 |
## STDOUT:
|
| 341 |
abc
|
| 342 |
## END
|
| 343 |
|
| 344 |
# ==============================================================================
|
| 345 |
# SECTION 3: BASIC SHELL FEATURES
|
| 346 |
# ==============================================================================
|
| 347 |
|
| 348 |
|
| 349 |
echo 'Single quotes preserve $variables and \backslashes'
|
| 350 |
## STDOUT:
|
| 351 |
Single quotes preserve $variables and \backslashes
|
| 352 |
## END
|
| 353 |
|
| 354 |
|
| 355 |
foo="bar"
|
| 356 |
echo "Double quotes expand \$foo: $foo"
|
| 357 |
## STDOUT:
|
| 358 |
Double quotes expand $foo: bar
|
| 359 |
## END
|
| 360 |
|
| 361 |
|
| 362 |
echo $'Line1\nLine2'
|
| 363 |
## STDOUT:
|
| 364 |
Line1
|
| 365 |
Line2
|
| 366 |
## END
|
| 367 |
|
| 368 |
|
| 369 |
true && echo "and_run"
|
| 370 |
false || echo "or_run"
|
| 371 |
## STDOUT:
|
| 372 |
and_run
|
| 373 |
or_run
|
| 374 |
## END
|
| 375 |
|
| 376 |
|
| 377 |
for i in 1 2; do echo $i; done
|
| 378 |
## STDOUT:
|
| 379 |
1
|
| 380 |
2
|
| 381 |
## END
|
| 382 |
|
| 383 |
|
| 384 |
x=2
|
| 385 |
while [ $x -gt 0 ]; do echo $x; x=$((x-1)); done
|
| 386 |
## STDOUT:
|
| 387 |
2
|
| 388 |
1
|
| 389 |
## END
|
| 390 |
|
| 391 |
|
| 392 |
if true; then echo yes; else echo no; fi
|
| 393 |
## STDOUT:
|
| 394 |
yes
|
| 395 |
## END
|
| 396 |
|
| 397 |
|
| 398 |
case "match" in
|
| 399 |
ma*) echo "ok" ;;
|
| 400 |
*) echo "fail" ;;
|
| 401 |
esac
|
| 402 |
## STDOUT:
|
| 403 |
ok
|
| 404 |
## END
|
| 405 |
|
| 406 |
|
| 407 |
echo {a,b,c}
|
| 408 |
## STDOUT:
|
| 409 |
a b c
|
| 410 |
## END
|
| 411 |
|
| 412 |
|
| 413 |
unset v
|
| 414 |
echo ${v:-def}
|
| 415 |
## STDOUT:
|
| 416 |
def
|
| 417 |
## END
|
| 418 |
|
| 419 |
|
| 420 |
p="a/b/c"
|
| 421 |
echo ${p##*/}
|
| 422 |
## STDOUT:
|
| 423 |
c
|
| 424 |
## END
|
| 425 |
|
| 426 |
|
| 427 |
echo $(echo hi)
|
| 428 |
## STDOUT:
|
| 429 |
hi
|
| 430 |
## END
|
| 431 |
|
| 432 |
|
| 433 |
echo $(( 1 + 2 ))
|
| 434 |
## STDOUT:
|
| 435 |
3
|
| 436 |
## END
|
| 437 |
|
| 438 |
|
| 439 |
echo "content" > tmp_out.txt
|
| 440 |
cat tmp_out.txt
|
| 441 |
rm tmp_out.txt
|
| 442 |
## STDOUT:
|
| 443 |
content
|
| 444 |
## END
|
| 445 |
|
| 446 |
|
| 447 |
cat <<EOF
|
| 448 |
line1
|
| 449 |
line2
|
| 450 |
EOF
|
| 451 |
## STDOUT:
|
| 452 |
line1
|
| 453 |
line2
|
| 454 |
## END
|
| 455 |
|
| 456 |
# ==============================================================================
|
| 457 |
# SECTION 4: SHELL BUILTIN COMMANDS
|
| 458 |
# ==============================================================================
|
| 459 |
|
| 460 |
|
| 461 |
# We use a subshell to avoid affecting the test runner's CWD
|
| 462 |
(
|
| 463 |
cd /
|
| 464 |
pwd
|
| 465 |
)
|
| 466 |
## STDOUT:
|
| 467 |
/
|
| 468 |
## END
|
| 469 |
|
| 470 |
|
| 471 |
# eval concatenates arguments and executes them
|
| 472 |
x="echo eval_works"
|
| 473 |
eval $x
|
| 474 |
## STDOUT:
|
| 475 |
eval_works
|
| 476 |
## END
|
| 477 |
|
| 478 |
|
| 479 |
export VAR_EXPORTED="visible"
|
| 480 |
$SH -c 'echo $VAR_EXPORTED'
|
| 481 |
## STDOUT:
|
| 482 |
visible
|
| 483 |
## END
|
| 484 |
|
| 485 |
|
| 486 |
# Read from stdin
|
| 487 |
echo "input_line" | {
|
| 488 |
read line
|
| 489 |
echo "Read: $line"
|
| 490 |
}
|
| 491 |
## STDOUT:
|
| 492 |
Read: input_line
|
| 493 |
## END
|
| 494 |
|
| 495 |
|
| 496 |
# set -- changes positional parameters
|
| 497 |
set -- arg1 arg2
|
| 498 |
echo "$1 $2 $#"
|
| 499 |
## STDOUT:
|
| 500 |
arg1 arg2 2
|
| 501 |
## END
|
| 502 |
|
| 503 |
|
| 504 |
set -- a b c
|
| 505 |
shift
|
| 506 |
echo "$1"
|
| 507 |
## STDOUT:
|
| 508 |
b
|
| 509 |
## END
|
| 510 |
|
| 511 |
|
| 512 |
# Basic trap on EXIT
|
| 513 |
(
|
| 514 |
trap 'echo exiting' EXIT
|
| 515 |
echo running
|
| 516 |
)
|
| 517 |
## STDOUT:
|
| 518 |
running
|
| 519 |
exiting
|
| 520 |
## END
|
| 521 |
|
| 522 |
|
| 523 |
x=10
|
| 524 |
unset x
|
| 525 |
echo "x is: ${x:-unset}"
|
| 526 |
## STDOUT:
|
| 527 |
x is: unset
|
| 528 |
## END
|
| 529 |
|
| 530 |
|
| 531 |
# Note: aliases are often disabled in non-interactive shells unless enabled
|
| 532 |
shopt -s expand_aliases
|
| 533 |
alias myecho='echo alias_executed'
|
| 534 |
myecho
|
| 535 |
## STDOUT:
|
| 536 |
alias_executed
|
| 537 |
## END
|
| 538 |
|
| 539 |
|
| 540 |
# command -v prints the path or description
|
| 541 |
command -v echo > /dev/null && echo "found"
|
| 542 |
## STDOUT:
|
| 543 |
found
|
| 544 |
## END
|
| 545 |
|
| 546 |
|
| 547 |
printf "Val: %d\n" 42
|
| 548 |
## STDOUT:
|
| 549 |
Val: 42
|
| 550 |
## END
|
| 551 |
|
| 552 |
|
| 553 |
# type describes how a command would be interpreted
|
| 554 |
type type | grep -q "builtin" && echo "ok"
|
| 555 |
## STDOUT:
|
| 556 |
ok
|
| 557 |
## END
|
| 558 |
|
| 559 |
# ==============================================================================
|
| 560 |
# SECTION 5: SHELL VARIABLES
|
| 561 |
# ==============================================================================
|
| 562 |
|
| 563 |
|
| 564 |
# Input Field Separator determines splitting
|
| 565 |
old_ifs="$IFS"
|
| 566 |
IFS=":"
|
| 567 |
x="a:b:c"
|
| 568 |
set -- $x
|
| 569 |
echo "$1 $2 $3"
|
| 570 |
IFS="$old_ifs"
|
| 571 |
## STDOUT:
|
| 572 |
a b c
|
| 573 |
## END
|
| 574 |
|
| 575 |
|
| 576 |
# RANDOM generates an integer 0-32767
|
| 577 |
a=$RANDOM
|
| 578 |
b=$RANDOM
|
| 579 |
# Verify they are numbers and likely different (though collision possible)
|
| 580 |
if [[ "$a" =~ ^[0-9]+$ ]] && [[ "$b" =~ ^[0-9]+$ ]]; then
|
| 581 |
echo "integers"
|
| 582 |
fi
|
| 583 |
## STDOUT:
|
| 584 |
integers
|
| 585 |
## END
|
| 586 |
|
| 587 |
|
| 588 |
# Array containing exit status of processes in the last pipeline
|
| 589 |
true | false | true
|
| 590 |
echo "${PIPESTATUS[0]} ${PIPESTATUS[1]} ${PIPESTATUS[2]}"
|
| 591 |
## STDOUT:
|
| 592 |
0 1 0
|
| 593 |
## END
|
| 594 |
|
| 595 |
# ==============================================================================
|
| 596 |
# SECTION 6: BASH FEATURES
|
| 597 |
# ==============================================================================
|
| 598 |
|
| 599 |
|
| 600 |
# String comparison
|
| 601 |
if [[ "abc" == "abc" ]]; then echo equal; fi
|
| 602 |
if [[ "abc" != "def" ]]; then echo diff; fi
|
| 603 |
## STDOUT:
|
| 604 |
equal
|
| 605 |
diff
|
| 606 |
## END
|
| 607 |
|
| 608 |
|
| 609 |
if [[ "foobar" == foo* ]]; then echo match; fi
|
| 610 |
## STDOUT:
|
| 611 |
match
|
| 612 |
## END
|
| 613 |
|
| 614 |
|
| 615 |
if [[ -n "x" && 1 -eq 1 ]]; then echo yes; fi
|
| 616 |
## STDOUT:
|
| 617 |
yes
|
| 618 |
## END
|
| 619 |
|
| 620 |
|
| 621 |
# C-style arithmetic
|
| 622 |
(( a = 1 + 2 ))
|
| 623 |
echo $a
|
| 624 |
(( a++ ))
|
| 625 |
echo $a
|
| 626 |
## STDOUT:
|
| 627 |
3
|
| 628 |
4
|
| 629 |
## END
|
| 630 |
|
| 631 |
|
| 632 |
(( x = 1 ? 10 : 20 ))
|
| 633 |
echo $x
|
| 634 |
## STDOUT:
|
| 635 |
10
|
| 636 |
## END
|
| 637 |
|
| 638 |
|
| 639 |
# Basic indexed array assignment and access
|
| 640 |
a[0]=zero
|
| 641 |
a[1]=one
|
| 642 |
echo "${a[0]} ${a[1]}"
|
| 643 |
## STDOUT:
|
| 644 |
zero one
|
| 645 |
## END
|
| 646 |
|
| 647 |
|
| 648 |
b=(apple banana cherry)
|
| 649 |
echo "${b[1]}"
|
| 650 |
## STDOUT:
|
| 651 |
banana
|
| 652 |
## END
|
| 653 |
|
| 654 |
|
| 655 |
c=(a b c d)
|
| 656 |
echo "${#c[@]}"
|
| 657 |
## STDOUT:
|
| 658 |
4
|
| 659 |
## END
|
| 660 |
|
| 661 |
|
| 662 |
d=(one two three four)
|
| 663 |
# Expand starting at index 1, take 2 elements
|
| 664 |
echo "${d[@]:1:2}"
|
| 665 |
## STDOUT:
|
| 666 |
two three
|
| 667 |
## END
|
| 668 |
|
| 669 |
|
| 670 |
e=(first)
|
| 671 |
e+=(second)
|
| 672 |
echo "${e[@]}"
|
| 673 |
## STDOUT:
|
| 674 |
first second
|
| 675 |
## END
|
| 676 |
|
| 677 |
|
| 678 |
# <() is a Bash feature, not POSIX, but supported by OSH
|
| 679 |
cat <(echo internal)
|
| 680 |
## STDOUT:
|
| 681 |
internal
|
| 682 |
## END
|
| 683 |
|
| 684 |
# ==============================================================================
|
| 685 |
# SECTION 3: BASIC SHELL FEATURES
|
| 686 |
# ==============================================================================
|
| 687 |
|
| 688 |
# ------------------------------------------------------------------------------
|
| 689 |
# 3.1.2 Quoting
|
| 690 |
# ------------------------------------------------------------------------------
|
| 691 |
|
| 692 |
|
| 693 |
echo \* \? \[ \]
|
| 694 |
## STDOUT:
|
| 695 |
* ? [ ]
|
| 696 |
## END
|
| 697 |
|
| 698 |
|
| 699 |
# A backslash-newline pair is removed.
|
| 700 |
echo "Start \
|
| 701 |
End"
|
| 702 |
## STDOUT:
|
| 703 |
Start End
|
| 704 |
## END
|
| 705 |
|
| 706 |
|
| 707 |
echo 'A'\''B'
|
| 708 |
## STDOUT:
|
| 709 |
A'B
|
| 710 |
## END
|
| 711 |
|
| 712 |
|
| 713 |
v="val"
|
| 714 |
echo "A $v B"
|
| 715 |
## STDOUT:
|
| 716 |
A val B
|
| 717 |
## END
|
| 718 |
|
| 719 |
|
| 720 |
echo "Date: $(echo date)"
|
| 721 |
## STDOUT:
|
| 722 |
Date: date
|
| 723 |
## END
|
| 724 |
|
| 725 |
|
| 726 |
set -- x y
|
| 727 |
echo "$1 $2"
|
| 728 |
## STDOUT:
|
| 729 |
x y
|
| 730 |
## END
|
| 731 |
|
| 732 |
|
| 733 |
# \a is alert, \b is backspace
|
| 734 |
# We pipe to 'cat -v' logic equivalent or just check length?
|
| 735 |
# Printing non-printing chars is flaky in tests. We check hex/octal.
|
| 736 |
echo $'\x41\065'
|
| 737 |
## STDOUT:
|
| 738 |
A5
|
| 739 |
## END
|
| 740 |
|
| 741 |
# ------------------------------------------------------------------------------
|
| 742 |
# 3.2.4 Lists of Commands
|
| 743 |
# ------------------------------------------------------------------------------
|
| 744 |
|
| 745 |
|
| 746 |
echo 1; echo 2; echo 3
|
| 747 |
## STDOUT:
|
| 748 |
1
|
| 749 |
2
|
| 750 |
3
|
| 751 |
## END
|
| 752 |
|
| 753 |
|
| 754 |
# We wait for the specific PID to ensure deterministic output order
|
| 755 |
{ echo async; } &
|
| 756 |
wait $!
|
| 757 |
## STDOUT:
|
| 758 |
async
|
| 759 |
## END
|
| 760 |
|
| 761 |
|
| 762 |
true && true && echo yes
|
| 763 |
## STDOUT:
|
| 764 |
yes
|
| 765 |
## END
|
| 766 |
|
| 767 |
|
| 768 |
false || false || echo yes
|
| 769 |
## STDOUT:
|
| 770 |
yes
|
| 771 |
## END
|
| 772 |
|
| 773 |
|
| 774 |
true && false || echo recovered
|
| 775 |
## STDOUT:
|
| 776 |
recovered
|
| 777 |
## END
|
| 778 |
|
| 779 |
|
| 780 |
# && and || have equal precedence and are left-associative
|
| 781 |
true || echo no && echo yes
|
| 782 |
## STDOUT:
|
| 783 |
yes
|
| 784 |
## END
|
| 785 |
|
| 786 |
# ------------------------------------------------------------------------------
|
| 787 |
# 3.2.5 Compound Commands
|
| 788 |
# ------------------------------------------------------------------------------
|
| 789 |
|
| 790 |
|
| 791 |
for (( i=0; i<3; i++ )); do echo $i; done
|
| 792 |
## STDOUT:
|
| 793 |
0
|
| 794 |
1
|
| 795 |
2
|
| 796 |
## END
|
| 797 |
|
| 798 |
|
| 799 |
for x in a b; do
|
| 800 |
for y in 1 2; do
|
| 801 |
echo $x$y
|
| 802 |
done
|
| 803 |
done
|
| 804 |
## STDOUT:
|
| 805 |
a1
|
| 806 |
a2
|
| 807 |
b1
|
| 808 |
b2
|
| 809 |
## END
|
| 810 |
|
| 811 |
|
| 812 |
for x in a; do
|
| 813 |
for y in b; do
|
| 814 |
break 2
|
| 815 |
echo fail_inner
|
| 816 |
done
|
| 817 |
echo fail_outer
|
| 818 |
done
|
| 819 |
echo done
|
| 820 |
## STDOUT:
|
| 821 |
done
|
| 822 |
## END
|
| 823 |
|
| 824 |
|
| 825 |
for i in 1 2 3; do
|
| 826 |
if [ $i -eq 2 ]; then continue; fi
|
| 827 |
echo $i
|
| 828 |
done
|
| 829 |
## STDOUT:
|
| 830 |
1
|
| 831 |
3
|
| 832 |
## END
|
| 833 |
|
| 834 |
|
| 835 |
x=3
|
| 836 |
if [ $x -eq 1 ]; then echo 1
|
| 837 |
elif [ $x -eq 2 ]; then echo 2
|
| 838 |
elif [ $x -eq 3 ]; then echo 3
|
| 839 |
else echo other
|
| 840 |
fi
|
| 841 |
## STDOUT:
|
| 842 |
3
|
| 843 |
## END
|
| 844 |
|
| 845 |
|
| 846 |
# Test | in patterns
|
| 847 |
case "b" in
|
| 848 |
a|b|c) echo match ;;
|
| 849 |
*) echo no ;;
|
| 850 |
esac
|
| 851 |
## STDOUT:
|
| 852 |
match
|
| 853 |
## END
|
| 854 |
|
| 855 |
|
| 856 |
# Bash 4.0 feature
|
| 857 |
case "start" in
|
| 858 |
start) echo -n "S" ;&
|
| 859 |
middle) echo -n "M" ;;
|
| 860 |
*) echo "F" ;;
|
| 861 |
esac
|
| 862 |
echo
|
| 863 |
## STDOUT:
|
| 864 |
SM
|
| 865 |
## END
|
| 866 |
|
| 867 |
# ------------------------------------------------------------------------------
|
| 868 |
# 3.5 Shell Expansions
|
| 869 |
# ------------------------------------------------------------------------------
|
| 870 |
|
| 871 |
|
| 872 |
echo {1..5}
|
| 873 |
## STDOUT:
|
| 874 |
1 2 3 4 5
|
| 875 |
## END
|
| 876 |
|
| 877 |
|
| 878 |
# {start..end..incr} (Bash 4.0)
|
| 879 |
echo {1..10..2}
|
| 880 |
## STDOUT:
|
| 881 |
1 3 5 7 9
|
| 882 |
## END
|
| 883 |
|
| 884 |
|
| 885 |
echo {5..1}
|
| 886 |
## STDOUT:
|
| 887 |
5 4 3 2 1
|
| 888 |
## END
|
| 889 |
|
| 890 |
|
| 891 |
echo {01..03}
|
| 892 |
## STDOUT:
|
| 893 |
01 02 03
|
| 894 |
## END
|
| 895 |
|
| 896 |
|
| 897 |
echo PRE{a,b}POST
|
| 898 |
## STDOUT:
|
| 899 |
PREaPOST PREbPOST
|
| 900 |
## END
|
| 901 |
|
| 902 |
# ------------------------------------------------------------------------------
|
| 903 |
# 3.5.3 Parameter Expansion
|
| 904 |
# ------------------------------------------------------------------------------
|
| 905 |
|
| 906 |
|
| 907 |
unset v
|
| 908 |
echo "${v:-default}"
|
| 909 |
v=""
|
| 910 |
echo "${v:-default}"
|
| 911 |
v="val"
|
| 912 |
echo "${v:-default}"
|
| 913 |
## STDOUT:
|
| 914 |
default
|
| 915 |
default
|
| 916 |
val
|
| 917 |
## END
|
| 918 |
|
| 919 |
|
| 920 |
unset v
|
| 921 |
echo "${v:=assigned}"
|
| 922 |
echo "$v"
|
| 923 |
## STDOUT:
|
| 924 |
assigned
|
| 925 |
assigned
|
| 926 |
## END
|
| 927 |
|
| 928 |
|
| 929 |
# Run in subshell because it exits
|
| 930 |
( unset v; echo "${v:?error_msg}" ) 2>&1 | grep -o "error_msg"
|
| 931 |
## STDOUT:
|
| 932 |
error_msg
|
| 933 |
## END
|
| 934 |
|
| 935 |
|
| 936 |
unset v
|
| 937 |
echo "1: <${v:+alt}>"
|
| 938 |
v="val"
|
| 939 |
echo "2: <${v:+alt}>"
|
| 940 |
## STDOUT:
|
| 941 |
1: <>
|
| 942 |
2: <alt>
|
| 943 |
## END
|
| 944 |
|
| 945 |
|
| 946 |
str="abcdef"
|
| 947 |
echo ${#str}
|
| 948 |
## STDOUT:
|
| 949 |
6
|
| 950 |
## END
|
| 951 |
|
| 952 |
|
| 953 |
p="path/to/file"
|
| 954 |
echo ${p#*/}
|
| 955 |
echo ${p##*/}
|
| 956 |
## STDOUT:
|
| 957 |
to/file
|
| 958 |
file
|
| 959 |
## END
|
| 960 |
|
| 961 |
|
| 962 |
f="file.tar.gz"
|
| 963 |
echo ${f%.*}
|
| 964 |
echo ${f%%.*}
|
| 965 |
## STDOUT:
|
| 966 |
file.tar
|
| 967 |
file
|
| 968 |
## END
|
| 969 |
|
| 970 |
|
| 971 |
s="0123456789"
|
| 972 |
echo ${s:7}
|
| 973 |
echo ${s: -3}
|
| 974 |
## STDOUT:
|
| 975 |
789
|
| 976 |
789
|
| 977 |
## END
|
| 978 |
|
| 979 |
|
| 980 |
s="0123456789"
|
| 981 |
echo ${s:1:3}
|
| 982 |
## STDOUT:
|
| 983 |
123
|
| 984 |
## END
|
| 985 |
|
| 986 |
|
| 987 |
s="bar bar"
|
| 988 |
echo ${s/r/z}
|
| 989 |
## STDOUT:
|
| 990 |
baz bar
|
| 991 |
## END
|
| 992 |
|
| 993 |
|
| 994 |
s="bar bar"
|
| 995 |
echo ${s//r/z}
|
| 996 |
## STDOUT:
|
| 997 |
baz baz
|
| 998 |
## END
|
| 999 |
|
| 1000 |
|
| 1001 |
s="foobarfoo"
|
| 1002 |
echo ${s/#foo/bar}
|
| 1003 |
echo ${s/%foo/bar}
|
| 1004 |
## STDOUT:
|
| 1005 |
barbarfoo
|
| 1006 |
foobarbar
|
| 1007 |
## END
|
| 1008 |
|
| 1009 |
|
| 1010 |
l="lowercase"
|
| 1011 |
u="UPPERCASE"
|
| 1012 |
echo ${l^}
|
| 1013 |
echo ${l^^}
|
| 1014 |
echo ${u,}
|
| 1015 |
echo ${u,,}
|
| 1016 |
## STDOUT:
|
| 1017 |
Lowercase
|
| 1018 |
LOWERCASE
|
| 1019 |
uPPERCASE
|
| 1020 |
uppercase
|
| 1021 |
## END
|
| 1022 |
|
| 1023 |
# ------------------------------------------------------------------------------
|
| 1024 |
# 3.5.5 Arithmetic Expansion
|
| 1025 |
# ------------------------------------------------------------------------------
|
| 1026 |
|
| 1027 |
|
| 1028 |
x=5
|
| 1029 |
echo $(( ++x ))
|
| 1030 |
echo $x
|
| 1031 |
## STDOUT:
|
| 1032 |
6
|
| 1033 |
6
|
| 1034 |
## END
|
| 1035 |
|
| 1036 |
|
| 1037 |
x=5
|
| 1038 |
echo $(( x++ ))
|
| 1039 |
echo $x
|
| 1040 |
## STDOUT:
|
| 1041 |
5
|
| 1042 |
6
|
| 1043 |
## END
|
| 1044 |
|
| 1045 |
|
| 1046 |
echo $(( 1 << 2 ))
|
| 1047 |
echo $(( 8 >> 1 ))
|
| 1048 |
echo $(( 3 & 1 ))
|
| 1049 |
echo $(( 3 | 4 ))
|
| 1050 |
## STDOUT:
|
| 1051 |
4
|
| 1052 |
4
|
| 1053 |
1
|
| 1054 |
7
|
| 1055 |
## END
|
| 1056 |
|
| 1057 |
|
| 1058 |
echo $(( 1 && 0 ))
|
| 1059 |
echo $(( 1 || 0 ))
|
| 1060 |
## STDOUT:
|
| 1061 |
0
|
| 1062 |
1
|
| 1063 |
## END
|
| 1064 |
|
| 1065 |
|
| 1066 |
echo $(( a=1+1, b=a+2 ))
|
| 1067 |
## STDOUT:
|
| 1068 |
4
|
| 1069 |
## END
|
| 1070 |
|
| 1071 |
# ==============================================================================
|
| 1072 |
# SECTION 4: SHELL BUILTIN COMMANDS
|
| 1073 |
# ==============================================================================
|
| 1074 |
|
| 1075 |
|
| 1076 |
# Assumes we are in a directory structure we can't guarantee?
|
| 1077 |
# We use . and ..
|
| 1078 |
pwd_orig=$(pwd)
|
| 1079 |
cd .
|
| 1080 |
[ "$(pwd)" = "$pwd_orig" ] && echo match
|
| 1081 |
## STDOUT:
|
| 1082 |
match
|
| 1083 |
## END
|
| 1084 |
|
| 1085 |
|
| 1086 |
x='$y'
|
| 1087 |
y='hello'
|
| 1088 |
eval echo $x
|
| 1089 |
## STDOUT:
|
| 1090 |
hello
|
| 1091 |
## END
|
| 1092 |
|
| 1093 |
|
| 1094 |
# exec with no command changes redirections for current shell
|
| 1095 |
(
|
| 1096 |
exec > tmp_exec.txt
|
| 1097 |
echo "content"
|
| 1098 |
)
|
| 1099 |
cat tmp_exec.txt
|
| 1100 |
rm tmp_exec.txt
|
| 1101 |
## STDOUT:
|
| 1102 |
content
|
| 1103 |
## END
|
| 1104 |
|
| 1105 |
|
| 1106 |
( exit 42; echo "no" )
|
| 1107 |
echo $?
|
| 1108 |
## STDOUT:
|
| 1109 |
42
|
| 1110 |
## END
|
| 1111 |
|
| 1112 |
|
| 1113 |
export TEST_VAR=exported
|
| 1114 |
$SH -c 'echo $TEST_VAR'
|
| 1115 |
## STDOUT:
|
| 1116 |
exported
|
| 1117 |
## END
|
| 1118 |
|
| 1119 |
|
| 1120 |
# Default read behavior handles backslashes as escape
|
| 1121 |
echo 'a\b' | { read line; echo "$line"; }
|
| 1122 |
## STDOUT:
|
| 1123 |
ab
|
| 1124 |
## END
|
| 1125 |
|
| 1126 |
|
| 1127 |
# -r preserves backslashes
|
| 1128 |
echo 'a\b' | { read -r line; echo "$line"; }
|
| 1129 |
## STDOUT:
|
| 1130 |
a\b
|
| 1131 |
## END
|
| 1132 |
|
| 1133 |
|
| 1134 |
set -- a b c d e
|
| 1135 |
shift 2
|
| 1136 |
echo "$*"
|
| 1137 |
## STDOUT:
|
| 1138 |
c d e
|
| 1139 |
## END
|
| 1140 |
|
| 1141 |
|
| 1142 |
# Simple trap test that doesn't rely on signal race conditions
|
| 1143 |
(
|
| 1144 |
trap 'echo caught' EXIT
|
| 1145 |
exit 0
|
| 1146 |
)
|
| 1147 |
## STDOUT:
|
| 1148 |
caught
|
| 1149 |
## END
|
| 1150 |
|
| 1151 |
|
| 1152 |
declare -i integer
|
| 1153 |
integer="10+5"
|
| 1154 |
echo $integer
|
| 1155 |
## STDOUT:
|
| 1156 |
15
|
| 1157 |
## END
|
| 1158 |
|
| 1159 |
|
| 1160 |
declare -r RO=1
|
| 1161 |
# Attempt to write should fail (status 1)
|
| 1162 |
( RO=2 ) 2>/dev/null || echo "failed"
|
| 1163 |
## STDOUT:
|
| 1164 |
failed
|
| 1165 |
## END
|
| 1166 |
|
| 1167 |
|
| 1168 |
f() {
|
| 1169 |
local v="inner"
|
| 1170 |
echo $v
|
| 1171 |
}
|
| 1172 |
v="outer"
|
| 1173 |
f
|
| 1174 |
echo $v
|
| 1175 |
## STDOUT:
|
| 1176 |
inner
|
| 1177 |
outer
|
| 1178 |
## END
|
| 1179 |
|
| 1180 |
|
| 1181 |
printf "|%5s|\n" "a"
|
| 1182 |
printf "|%-5s|\n" "a"
|
| 1183 |
## STDOUT:
|
| 1184 |
| a|
|
| 1185 |
|a |
|
| 1186 |
## END
|
| 1187 |
|
| 1188 |
|
| 1189 |
# Note: globstar behavior is complex, just testing it can be set
|
| 1190 |
shopt -s globstar
|
| 1191 |
shopt -q globstar && echo "set"
|
| 1192 |
shopt -u globstar
|
| 1193 |
shopt -q globstar || echo "unset"
|
| 1194 |
## STDOUT:
|
| 1195 |
set
|
| 1196 |
unset
|
| 1197 |
## END
|
| 1198 |
|
| 1199 |
# ==============================================================================
|
| 1200 |
# SECTION 5: SHELL VARIABLES
|
| 1201 |
# ==============================================================================
|
| 1202 |
|
| 1203 |
|
| 1204 |
# OSH should support this. We check it's non-empty and changes.
|
| 1205 |
r1=$RANDOM
|
| 1206 |
r2=$RANDOM
|
| 1207 |
[ -n "$r1" ] && echo "ok"
|
| 1208 |
# It is statistically improbable for them to match, but possible.
|
| 1209 |
[ "$r1" != "$r2" ] || echo "collision"
|
| 1210 |
## STDOUT:
|
| 1211 |
ok
|
| 1212 |
## END
|
| 1213 |
|
| 1214 |
|
| 1215 |
# Approximate check
|
| 1216 |
sleep 1
|
| 1217 |
if [ "$SECONDS" -ge 1 ]; then echo "ok"; fi
|
| 1218 |
## STDOUT:
|
| 1219 |
ok
|
| 1220 |
## END
|
| 1221 |
|
| 1222 |
|
| 1223 |
# Should be an integer
|
| 1224 |
case "$UID" in
|
| 1225 |
*[!0-9]*) echo fail ;;
|
| 1226 |
*) echo ok ;;
|
| 1227 |
esac
|
| 1228 |
## STDOUT:
|
| 1229 |
ok
|
| 1230 |
## END
|
| 1231 |
|
| 1232 |
# ==============================================================================
|
| 1233 |
# SECTION 6: BASH FEATURES
|
| 1234 |
# ==============================================================================
|
| 1235 |
|
| 1236 |
|
| 1237 |
# FIX: explicitly turn OFF first, then ON.
|
| 1238 |
set +o pipefail
|
| 1239 |
true | false | true
|
| 1240 |
echo "off: $?"
|
| 1241 |
set -o pipefail
|
| 1242 |
true | false | true
|
| 1243 |
echo "on: $?"
|
| 1244 |
## STDOUT:
|
| 1245 |
off: 0
|
| 1246 |
on: 1
|
| 1247 |
## END
|
| 1248 |
|
| 1249 |
# ------------------------------------------------------------------------------
|
| 1250 |
# 6.7 Arrays
|
| 1251 |
# ------------------------------------------------------------------------------
|
| 1252 |
|
| 1253 |
|
| 1254 |
a[0]=10
|
| 1255 |
a[2]=30
|
| 1256 |
echo ${a[0]}
|
| 1257 |
echo ${a[1]} # Empty
|
| 1258 |
echo ${a[2]}
|
| 1259 |
## STDOUT:
|
| 1260 |
10
|
| 1261 |
|
| 1262 |
30
|
| 1263 |
## END
|
| 1264 |
|
| 1265 |
|
| 1266 |
a=(x y z)
|
| 1267 |
echo "${a[@]}"
|
| 1268 |
## STDOUT:
|
| 1269 |
x y z
|
| 1270 |
## END
|
| 1271 |
|
| 1272 |
|
| 1273 |
a=(x y z)
|
| 1274 |
echo ${#a[@]}
|
| 1275 |
## STDOUT:
|
| 1276 |
3
|
| 1277 |
## END
|
| 1278 |
|
| 1279 |
|
| 1280 |
a=(a b c d e)
|
| 1281 |
echo "${a[@]:2:2}"
|
| 1282 |
## STDOUT:
|
| 1283 |
c d
|
| 1284 |
## END
|
| 1285 |
|
| 1286 |
|
| 1287 |
a=(a)
|
| 1288 |
a+=(b)
|
| 1289 |
a+=(c)
|
| 1290 |
echo "${a[@]}"
|
| 1291 |
## STDOUT:
|
| 1292 |
a b c
|
| 1293 |
## END
|
| 1294 |
|
| 1295 |
|
| 1296 |
# Requires declare -A
|
| 1297 |
declare -A dict
|
| 1298 |
dict[key]="value"
|
| 1299 |
dict[foo]="bar"
|
| 1300 |
echo "${dict[key]}"
|
| 1301 |
echo "${dict[foo]}"
|
| 1302 |
## STDOUT:
|
| 1303 |
value
|
| 1304 |
bar
|
| 1305 |
## END
|
| 1306 |
|
| 1307 |
|
| 1308 |
declare -A dict
|
| 1309 |
dict[a]=1
|
| 1310 |
dict[b]=2
|
| 1311 |
# Order is not guaranteed, so we sort output
|
| 1312 |
echo "${!dict[@]}" | tr ' ' '\n' | sort
|
| 1313 |
## STDOUT:
|
| 1314 |
a
|
| 1315 |
b
|
| 1316 |
## END
|
| 1317 |
|
| 1318 |
# ------------------------------------------------------------------------------
|
| 1319 |
# 6.4 Conditional Expressions [[ ]]
|
| 1320 |
# ------------------------------------------------------------------------------
|
| 1321 |
|
| 1322 |
|
| 1323 |
if [[ ! -z "content" ]]; then echo ok; fi
|
| 1324 |
## STDOUT:
|
| 1325 |
ok
|
| 1326 |
## END
|
| 1327 |
|
| 1328 |
|
| 1329 |
if [[ -n "a" && -n "b" ]]; then echo ok; fi
|
| 1330 |
## STDOUT:
|
| 1331 |
ok
|
| 1332 |
## END
|
| 1333 |
|
| 1334 |
|
| 1335 |
if [[ -z "a" || -n "b" ]]; then echo ok; fi
|
| 1336 |
## STDOUT:
|
| 1337 |
ok
|
| 1338 |
## END
|
| 1339 |
|
| 1340 |
|
| 1341 |
if [[ 10 -eq 10 ]]; then echo eq; fi
|
| 1342 |
if [[ 10 -ne 5 ]]; then echo ne; fi
|
| 1343 |
if [[ 5 -lt 10 ]]; then echo lt; fi
|
| 1344 |
if [[ 10 -gt 5 ]]; then echo gt; fi
|
| 1345 |
## STDOUT:
|
| 1346 |
eq
|
| 1347 |
ne
|
| 1348 |
lt
|
| 1349 |
gt
|
| 1350 |
## END
|
| 1351 |
|
| 1352 |
|
| 1353 |
# Basic regex support
|
| 1354 |
val="myfile.txt"
|
| 1355 |
if [[ "$val" =~ \.txt$ ]]; then echo match; fi
|
| 1356 |
## STDOUT:
|
| 1357 |
match
|
| 1358 |
## END
|
| 1359 |
|
| 1360 |
|
| 1361 |
val="foo:bar"
|
| 1362 |
if [[ "$val" =~ ^(.*):(.*)$ ]]; then
|
| 1363 |
echo "${BASH_REMATCH[1]}"
|
| 1364 |
echo "${BASH_REMATCH[2]}"
|
| 1365 |
fi
|
| 1366 |
## STDOUT:
|
| 1367 |
foo
|
| 1368 |
bar
|
| 1369 |
## END
|
| 1370 |
|
| 1371 |
# ------------------------------------------------------------------------------
|
| 1372 |
# 6.12 Shell Compatibility / Misc
|
| 1373 |
# ------------------------------------------------------------------------------
|
| 1374 |
|
| 1375 |
|
| 1376 |
myfunc() { echo called; }
|
| 1377 |
myfunc
|
| 1378 |
## STDOUT:
|
| 1379 |
called
|
| 1380 |
## END
|
| 1381 |
|
| 1382 |
|
| 1383 |
function myfunc_k { echo called; }
|
| 1384 |
myfunc_k
|
| 1385 |
## STDOUT:
|
| 1386 |
called
|
| 1387 |
## END
|
| 1388 |
|
| 1389 |
|
| 1390 |
cat <(echo "input")
|
| 1391 |
## STDOUT:
|
| 1392 |
input
|
| 1393 |
## END
|
| 1394 |
|
| 1395 |
|
| 1396 |
# Data written to the pipe is read by cat
|
| 1397 |
echo "data" > >(cat)
|
| 1398 |
wait
|
| 1399 |
## STDOUT:
|
| 1400 |
data
|
| 1401 |
## END
|
| 1402 |
|
| 1403 |
# ==============================================================================
|
| 1404 |
# 3.6 Redirections (Advanced)
|
| 1405 |
# ==============================================================================
|
| 1406 |
|
| 1407 |
|
| 1408 |
# Redirect stderr to stdout (standard), then stdout to /dev/null
|
| 1409 |
{ echo "stderr" >&2; echo "stdout"; } 2>&1 >/dev/null
|
| 1410 |
## STDOUT:
|
| 1411 |
stderr
|
| 1412 |
## END
|
| 1413 |
|
| 1414 |
|
| 1415 |
# Close stdout (fd 1) using >&-
|
| 1416 |
( echo "should not print" >&- ) 2>/dev/null || echo "write failed"
|
| 1417 |
## STDOUT:
|
| 1418 |
write failed
|
| 1419 |
## END
|
| 1420 |
|
| 1421 |
|
| 1422 |
# Move fd 1 to 5, write to 5, which goes to actual stdout
|
| 1423 |
# Note: Syntax 1>&5- is specific to moving.
|
| 1424 |
( echo "moved" 1>&5- ) 5>&1
|
| 1425 |
## STDOUT:
|
| 1426 |
moved
|
| 1427 |
## END
|
| 1428 |
|
| 1429 |
|
| 1430 |
# Open fd 3 for read/write on a file (exec <> file)
|
| 1431 |
echo "content" > rw_test.txt
|
| 1432 |
exec 3<> rw_test.txt
|
| 1433 |
read -u 3 line
|
| 1434 |
echo "Read: $line"
|
| 1435 |
echo "append" >&3
|
| 1436 |
exec 3>&-
|
| 1437 |
cat rw_test.txt
|
| 1438 |
rm rw_test.txt
|
| 1439 |
## STDOUT:
|
| 1440 |
Read: content
|
| 1441 |
content
|
| 1442 |
append
|
| 1443 |
## END
|
| 1444 |
|
| 1445 |
# ==============================================================================
|
| 1446 |
# 3.7 Executing Commands
|
| 1447 |
# ==============================================================================
|
| 1448 |
|
| 1449 |
|
| 1450 |
# Assignment preceding command affects only that command
|
| 1451 |
x=global
|
| 1452 |
x=local sh -c 'echo $x'
|
| 1453 |
echo $x
|
| 1454 |
## STDOUT:
|
| 1455 |
local
|
| 1456 |
global
|
| 1457 |
## END
|
| 1458 |
|
| 1459 |
|
| 1460 |
# Exported variables are inherited
|
| 1461 |
export MY_ENV_VAR="inherited"
|
| 1462 |
$SH -c 'echo $MY_ENV_VAR'
|
| 1463 |
## STDOUT:
|
| 1464 |
inherited
|
| 1465 |
## END
|
| 1466 |
|
| 1467 |
|
| 1468 |
# 127 for command not found
|
| 1469 |
non_existent_command_ZZZ 2>/dev/null
|
| 1470 |
echo $?
|
| 1471 |
## STDOUT:
|
| 1472 |
127
|
| 1473 |
## END
|
| 1474 |
|
| 1475 |
# ==============================================================================
|
| 1476 |
# 4. Shell Builtin Commands
|
| 1477 |
# ==============================================================================
|
| 1478 |
|
| 1479 |
|
| 1480 |
# Parse arguments
|
| 1481 |
set -- -a -b val arg
|
| 1482 |
while getopts "ab:" opt; do
|
| 1483 |
case $opt in
|
| 1484 |
a) echo "flag a" ;;
|
| 1485 |
b) echo "flag b with $OPTARG" ;;
|
| 1486 |
esac
|
| 1487 |
done
|
| 1488 |
shift $((OPTIND-1))
|
| 1489 |
echo "remain: $1"
|
| 1490 |
## STDOUT:
|
| 1491 |
flag a
|
| 1492 |
flag b with val
|
| 1493 |
remain: arg
|
| 1494 |
## END
|
| 1495 |
|
| 1496 |
|
| 1497 |
# Verify umask sets permissions (mocking with printing)
|
| 1498 |
# Saving/restoring is good practice
|
| 1499 |
old_umask=$(umask)
|
| 1500 |
umask 022
|
| 1501 |
umask
|
| 1502 |
umask $old_umask
|
| 1503 |
## STDOUT:
|
| 1504 |
0022
|
| 1505 |
## END
|
| 1506 |
|
| 1507 |
|
| 1508 |
# Read lines into indexed array
|
| 1509 |
printf "line1\nline2\n" > mapfile_test.txt
|
| 1510 |
mapfile -t lines < mapfile_test.txt
|
| 1511 |
echo "${lines[0]}"
|
| 1512 |
echo "${lines[1]}"
|
| 1513 |
rm mapfile_test.txt
|
| 1514 |
## STDOUT:
|
| 1515 |
line1
|
| 1516 |
line2
|
| 1517 |
## END
|
| 1518 |
|
| 1519 |
|
| 1520 |
# Remember command locations
|
| 1521 |
hash >/dev/null 2>&1
|
| 1522 |
# Just check exit code implies success or empty
|
| 1523 |
echo $?
|
| 1524 |
## STDOUT:
|
| 1525 |
0
|
| 1526 |
## END
|
| 1527 |
|
| 1528 |
|
| 1529 |
# Error on unset variables
|
| 1530 |
set -u
|
| 1531 |
( echo $UNSET_VAR ) 2>/dev/null && echo "fail" || echo "caught"
|
| 1532 |
## STDOUT:
|
| 1533 |
caught
|
| 1534 |
## END
|
| 1535 |
|
| 1536 |
|
| 1537 |
# Prevent overwriting files
|
| 1538 |
echo "data" > noclobber.txt
|
| 1539 |
set -C
|
| 1540 |
( echo "new" > noclobber.txt ) 2>/dev/null || echo "protected"
|
| 1541 |
set +C
|
| 1542 |
rm noclobber.txt
|
| 1543 |
## STDOUT:
|
| 1544 |
protected
|
| 1545 |
## END
|
| 1546 |
|
| 1547 |
|
| 1548 |
# Toggle a shell option
|
| 1549 |
shopt -s nullglob
|
| 1550 |
shopt -q nullglob && echo "on"
|
| 1551 |
shopt -u nullglob
|
| 1552 |
## STDOUT:
|
| 1553 |
on
|
| 1554 |
## END
|
| 1555 |
|
| 1556 |
# ==============================================================================
|
| 1557 |
# 5. Shell Variables
|
| 1558 |
# ==============================================================================
|
| 1559 |
|
| 1560 |
|
| 1561 |
# Variable name in a variable
|
| 1562 |
target="value"
|
| 1563 |
ptr="target"
|
| 1564 |
echo "${!ptr}"
|
| 1565 |
## STDOUT:
|
| 1566 |
value
|
| 1567 |
## END
|
| 1568 |
|
| 1569 |
|
| 1570 |
# Bash 4.3+ feature
|
| 1571 |
foo="bar"
|
| 1572 |
declare -n ref=foo
|
| 1573 |
echo $ref
|
| 1574 |
ref="changed"
|
| 1575 |
echo $foo
|
| 1576 |
## STDOUT:
|
| 1577 |
bar
|
| 1578 |
changed
|
| 1579 |
## END
|
| 1580 |
|
| 1581 |
# ==============================================================================
|
| 1582 |
# 6. Bash Features
|
| 1583 |
# ==============================================================================
|
| 1584 |
|
| 1585 |
|
| 1586 |
# In scripts, $- should not contain 'i' usually
|
| 1587 |
case "$-" in
|
| 1588 |
*i*) echo interactive ;;
|
| 1589 |
*) echo script ;;
|
| 1590 |
esac
|
| 1591 |
## STDOUT:
|
| 1592 |
script
|
| 1593 |
## END
|
| 1594 |
|
| 1595 |
|
| 1596 |
# Aliases are not expanded by default in non-interactive mode
|
| 1597 |
shopt -s expand_aliases
|
| 1598 |
alias foo='echo bar'
|
| 1599 |
foo
|
| 1600 |
## STDOUT:
|
| 1601 |
bar
|
| 1602 |
## END
|
| 1603 |
|
| 1604 |
|
| 1605 |
# We need a predictable directory structure. Using /tmp or .
|
| 1606 |
# OSH implements these.
|
| 1607 |
dirs -c # Clear stack
|
| 1608 |
pushd . >/dev/null
|
| 1609 |
pushd . >/dev/null
|
| 1610 |
# Stack should have 3 entries (original + 2 pushes)
|
| 1611 |
dirs | wc -w | tr -d ' '
|
| 1612 |
popd >/dev/null
|
| 1613 |
popd >/dev/null
|
| 1614 |
## STDOUT:
|
| 1615 |
3
|
| 1616 |
## END
|
| 1617 |
|
| 1618 |
# ==============================================================================
|
| 1619 |
# Appendix B: Major Differences From The Bourne Shell
|
| 1620 |
# ==============================================================================
|
| 1621 |
|
| 1622 |
|
| 1623 |
# Bourne shell did not support 'function name { ... }' syntax, Bash does.
|
| 1624 |
function bash_style {
|
| 1625 |
echo "works"
|
| 1626 |
}
|
| 1627 |
bash_style
|
| 1628 |
## STDOUT:
|
| 1629 |
works
|
| 1630 |
## END
|
| 1631 |
|
| 1632 |
|
| 1633 |
# 'select' is a Korn shell extension included in Bash
|
| 1634 |
# Tested in 3.2.5.2, but re-verifying structure
|
| 1635 |
select i in one; do echo $i; break; done <<EOF
|
| 1636 |
1
|
| 1637 |
EOF
|
| 1638 |
## STDOUT:
|
| 1639 |
one
|
| 1640 |
## END
|
| 1641 |
|
| 1642 |
|
| 1643 |
# 'time' is a reserved word, not just a command
|
| 1644 |
# It prints to stderr. We just check syntax doesn't crash.
|
| 1645 |
time true 2>/dev/null
|
| 1646 |
echo $?
|
| 1647 |
## STDOUT:
|
| 1648 |
0
|
| 1649 |
## END
|
| 1650 |
|
| 1651 |
|
| 1652 |
# '!' is a reserved word in Bash
|
| 1653 |
! false
|
| 1654 |
echo $?
|
| 1655 |
## STDOUT:
|
| 1656 |
0
|
| 1657 |
## END
|
| 1658 |
|
| 1659 |
# ==============================================================================
|
| 1660 |
# 3.5.7 Word Splitting
|
| 1661 |
# ==============================================================================
|
| 1662 |
|
| 1663 |
|
| 1664 |
# Default IFS is <space><tab><newline>
|
| 1665 |
# " The shell treats each character of IFS as a delimiter "
|
| 1666 |
x="a b c"
|
| 1667 |
set -- $x
|
| 1668 |
echo "$1|$2|$3"
|
| 1669 |
## STDOUT:
|
| 1670 |
a|b|c
|
| 1671 |
## END
|
| 1672 |
|
| 1673 |
|
| 1674 |
# " If the value of IFS is exactly <space><tab><newline> ... "
|
| 1675 |
# Here we change it to comma.
|
| 1676 |
IFS=,
|
| 1677 |
x="a,b,c"
|
| 1678 |
set -- $x
|
| 1679 |
echo "$1 $2 $3"
|
| 1680 |
## STDOUT:
|
| 1681 |
a b c
|
| 1682 |
## END
|
| 1683 |
|
| 1684 |
|
| 1685 |
# " If IFS is null, no word splitting occurs "
|
| 1686 |
IFS=
|
| 1687 |
x="a b c"
|
| 1688 |
set -- $x
|
| 1689 |
echo "$1"
|
| 1690 |
echo "$#"
|
| 1691 |
## STDOUT:
|
| 1692 |
a b c
|
| 1693 |
1
|
| 1694 |
## END
|
| 1695 |
|
| 1696 |
|
| 1697 |
# Non-whitespace IFS retains empty fields
|
| 1698 |
IFS=:
|
| 1699 |
x=":a::b:"
|
| 1700 |
set -- $x
|
| 1701 |
echo "count: $#"
|
| 1702 |
# Note: Behavior of trailing separators can vary, but standard checks:
|
| 1703 |
[ "$1" = "" ] && echo "empty first"
|
| 1704 |
[ "$2" = "a" ] && echo "second a"
|
| 1705 |
## STDOUT:
|
| 1706 |
count: 4
|
| 1707 |
empty first
|
| 1708 |
second a
|
| 1709 |
## END
|
| 1710 |
|
| 1711 |
# ==============================================================================
|
| 1712 |
# 3.5.8 Filename Expansion (Globbing)
|
| 1713 |
# ==============================================================================
|
| 1714 |
|
| 1715 |
|
| 1716 |
# * Matches any string, including null string.
|
| 1717 |
rm -f glob_test_*
|
| 1718 |
touch glob_test_1 glob_test_2
|
| 1719 |
echo glob_test_*
|
| 1720 |
rm glob_test_*
|
| 1721 |
## STDOUT:
|
| 1722 |
glob_test_1 glob_test_2
|
| 1723 |
## END
|
| 1724 |
|
| 1725 |
|
| 1726 |
# ? Matches any single character.
|
| 1727 |
rm -f glob_?
|
| 1728 |
touch glob_a glob_b
|
| 1729 |
echo glob_?
|
| 1730 |
rm glob_a glob_b
|
| 1731 |
## STDOUT:
|
| 1732 |
glob_a glob_b
|
| 1733 |
## END
|
| 1734 |
|
| 1735 |
|
| 1736 |
# Matches any one of the enclosed characters.
|
| 1737 |
rm -f glob_[ab]
|
| 1738 |
touch glob_a glob_b glob_c
|
| 1739 |
echo glob_[ab]
|
| 1740 |
rm glob_a glob_b glob_c
|
| 1741 |
## STDOUT:
|
| 1742 |
glob_a glob_b
|
| 1743 |
## END
|
| 1744 |
|
| 1745 |
# ==============================================================================
|
| 1746 |
# 3.5.9 Quote Removal
|
| 1747 |
# ==============================================================================
|
| 1748 |
|
| 1749 |
|
| 1750 |
# After expansions, unquoted quotes are removed.
|
| 1751 |
echo "a" 'b' \c
|
| 1752 |
## STDOUT:
|
| 1753 |
a b c
|
| 1754 |
## END
|
| 1755 |
|
| 1756 |
# ==============================================================================
|
| 1757 |
# 4. Shell Builtin Commands (Advanced Options)
|
| 1758 |
# ==============================================================================
|
| 1759 |
|
| 1760 |
|
| 1761 |
# Read words into an array.
|
| 1762 |
echo "one two three" | {
|
| 1763 |
read -a words
|
| 1764 |
echo "${words[1]}"
|
| 1765 |
}
|
| 1766 |
## STDOUT:
|
| 1767 |
two
|
| 1768 |
## END
|
| 1769 |
|
| 1770 |
|
| 1771 |
# Read until specific char.
|
| 1772 |
echo "line1;line2" | {
|
| 1773 |
read -d ";" first
|
| 1774 |
echo "$first"
|
| 1775 |
}
|
| 1776 |
## STDOUT:
|
| 1777 |
line1
|
| 1778 |
## END
|
| 1779 |
|
| 1780 |
|
| 1781 |
# Print output to a variable instead of stdout.
|
| 1782 |
printf -v myvar "value: %d" 10
|
| 1783 |
echo "$myvar"
|
| 1784 |
## STDOUT:
|
| 1785 |
value: 10
|
| 1786 |
## END
|
| 1787 |
|
| 1788 |
|
| 1789 |
# 'source' is a synonym for '.' in Bash.
|
| 1790 |
echo 'echo sourced' > tmp_source.sh
|
| 1791 |
source tmp_source.sh
|
| 1792 |
rm tmp_source.sh
|
| 1793 |
## STDOUT:
|
| 1794 |
sourced
|
| 1795 |
## END
|
| 1796 |
|
| 1797 |
|
| 1798 |
# unset -v unsets variables, -f unsets functions
|
| 1799 |
foo() { echo func; }
|
| 1800 |
foo="var"
|
| 1801 |
unset -v foo
|
| 1802 |
echo "${foo:-unset_var}"
|
| 1803 |
foo
|
| 1804 |
unset -f foo
|
| 1805 |
type foo >/dev/null 2>&1 || echo "unset_func"
|
| 1806 |
## STDOUT:
|
| 1807 |
unset_var
|
| 1808 |
func
|
| 1809 |
unset_func
|
| 1810 |
## END
|
| 1811 |
|
| 1812 |
# ==============================================================================
|
| 1813 |
# 6.4 Bash Conditional Expressions (File Operators)
|
| 1814 |
# ==============================================================================
|
| 1815 |
|
| 1816 |
|
| 1817 |
rm -rf test_dir test_file
|
| 1818 |
mkdir test_dir
|
| 1819 |
touch test_file
|
| 1820 |
if [[ -e test_file ]]; then echo "exists"; fi
|
| 1821 |
if [[ -f test_file ]]; then echo "file"; fi
|
| 1822 |
if [[ -d test_dir ]]; then echo "dir"; fi
|
| 1823 |
rm -rf test_dir test_file
|
| 1824 |
## STDOUT:
|
| 1825 |
exists
|
| 1826 |
file
|
| 1827 |
dir
|
| 1828 |
## END
|
| 1829 |
|
| 1830 |
|
| 1831 |
# True if file exists and is not empty.
|
| 1832 |
touch empty
|
| 1833 |
echo "data" > full
|
| 1834 |
if [[ ! -s empty ]]; then echo "empty is empty"; fi
|
| 1835 |
if [[ -s full ]]; then echo "full is full"; fi
|
| 1836 |
rm empty full
|
| 1837 |
## STDOUT:
|
| 1838 |
empty is empty
|
| 1839 |
full is full
|
| 1840 |
## END
|
| 1841 |
|
| 1842 |
|
| 1843 |
touch -t 202001010000 old
|
| 1844 |
touch -t 202001020000 new
|
| 1845 |
if [[ new -nt old ]]; then echo "new is newer"; fi
|
| 1846 |
rm old new
|
| 1847 |
## STDOUT:
|
| 1848 |
new is newer
|
| 1849 |
## END
|
| 1850 |
|
| 1851 |
|
| 1852 |
# Lexicographical comparison within [[ ]] uses current locale.
|
| 1853 |
if [[ "a" < "b" ]]; then echo "a < b"; fi
|
| 1854 |
if [[ "z" > "a" ]]; then echo "z > a"; fi
|
| 1855 |
## STDOUT:
|
| 1856 |
a < b
|
| 1857 |
z > a
|
| 1858 |
## END
|
| 1859 |
|
| 1860 |
# ==============================================================================
|
| 1861 |
# 6.5 Shell Arithmetic (Constants)
|
| 1862 |
# ==============================================================================
|
| 1863 |
|
| 1864 |
|
| 1865 |
# Constants with leading 0 are octal.
|
| 1866 |
echo $(( 010 ))
|
| 1867 |
## STDOUT:
|
| 1868 |
8
|
| 1869 |
## END
|
| 1870 |
|
| 1871 |
|
| 1872 |
# Constants with 0x are hex.
|
| 1873 |
echo $(( 0x10 ))
|
| 1874 |
## STDOUT:
|
| 1875 |
16
|
| 1876 |
## END
|
| 1877 |
|
| 1878 |
|
| 1879 |
# base#number syntax.
|
| 1880 |
echo $(( 2#101 ))
|
| 1881 |
echo $(( 16#A ))
|
| 1882 |
## STDOUT:
|
| 1883 |
5
|
| 1884 |
10
|
| 1885 |
## END
|
| 1886 |
|
| 1887 |
# ==============================================================================
|
| 1888 |
# 6.10 The Restricted Shell
|
| 1889 |
# ==============================================================================
|
| 1890 |
|
| 1891 |
|
| 1892 |
# Testing strictness of readonly variables
|
| 1893 |
readonly r=10
|
| 1894 |
# We expect this to fail gracefully (status 1) without crashing the shell
|
| 1895 |
( r=20 ) 2>/dev/null || echo "cannot assign"
|
| 1896 |
## STDOUT:
|
| 1897 |
cannot assign
|
| 1898 |
## END
|
| 1899 |
|
| 1900 |
# ==============================================================================
|
| 1901 |
# 7. Job Control Variables
|
| 1902 |
# ==============================================================================
|
| 1903 |
|
| 1904 |
|
| 1905 |
# PID of last background command.
|
| 1906 |
sleep 0.1 &
|
| 1907 |
pid=$!
|
| 1908 |
# Check if pid is an integer
|
| 1909 |
case $pid in
|
| 1910 |
*[!0-9]*) echo "fail" ;;
|
| 1911 |
*) echo "ok" ;;
|
| 1912 |
esac
|
| 1913 |
wait $pid
|
| 1914 |
## STDOUT:
|
| 1915 |
ok
|
| 1916 |
## END
|
| 1917 |
|
| 1918 |
# ==============================================================================
|
| 1919 |
# 3.2.5.2 Conditional Constructs (Case Fallthrough)
|
| 1920 |
# ==============================================================================
|
| 1921 |
|
| 1922 |
|
| 1923 |
# ;;& tests the next pattern after a match, rather than exiting
|
| 1924 |
x="a"
|
| 1925 |
case "$x" in
|
| 1926 |
a) echo -n "1" ;;&
|
| 1927 |
*) echo -n "2" ;;
|
| 1928 |
esac
|
| 1929 |
echo
|
| 1930 |
## STDOUT:
|
| 1931 |
12
|
| 1932 |
## END
|
| 1933 |
|
| 1934 |
# ==============================================================================
|
| 1935 |
# 3.6 Redirections (Special Syntax)
|
| 1936 |
# ==============================================================================
|
| 1937 |
|
| 1938 |
|
| 1939 |
# &>word is preferred to >word 2>&1
|
| 1940 |
{ echo "out"; echo "err" >&2; } &> combined.txt
|
| 1941 |
cat combined.txt | sort
|
| 1942 |
rm combined.txt
|
| 1943 |
## STDOUT:
|
| 1944 |
err
|
| 1945 |
out
|
| 1946 |
## END
|
| 1947 |
|
| 1948 |
|
| 1949 |
# <<- removes leading tab characters from input lines and the delimiter line
|
| 1950 |
# We use ANSI-C quoting with eval to ensure tabs are real in the test execution
|
| 1951 |
code=$'cat <<-EOF\n\tline1\n\tline2\nEOF'
|
| 1952 |
eval "$code"
|
| 1953 |
## STDOUT:
|
| 1954 |
line1
|
| 1955 |
line2
|
| 1956 |
## END
|
| 1957 |
|
| 1958 |
# ==============================================================================
|
| 1959 |
# 4. Shell Builtin Commands (Control Flow)
|
| 1960 |
# ==============================================================================
|
| 1961 |
|
| 1962 |
|
| 1963 |
# break n breaks out of n levels
|
| 1964 |
for i in 1; do
|
| 1965 |
for j in 1; do
|
| 1966 |
break 2
|
| 1967 |
echo "inner"
|
| 1968 |
done
|
| 1969 |
echo "outer"
|
| 1970 |
done
|
| 1971 |
echo "done"
|
| 1972 |
## STDOUT:
|
| 1973 |
done
|
| 1974 |
## END
|
| 1975 |
|
| 1976 |
|
| 1977 |
# continue n resumes at the nth enclosing loop
|
| 1978 |
for i in 1 2; do
|
| 1979 |
echo "start $i"
|
| 1980 |
for j in 1; do
|
| 1981 |
continue 2
|
| 1982 |
echo "inner"
|
| 1983 |
done
|
| 1984 |
echo "end $i"
|
| 1985 |
done
|
| 1986 |
## STDOUT:
|
| 1987 |
start 1
|
| 1988 |
start 2
|
| 1989 |
## END
|
| 1990 |
|
| 1991 |
|
| 1992 |
# return exits a function with value
|
| 1993 |
f() {
|
| 1994 |
return 42
|
| 1995 |
echo "unreachable"
|
| 1996 |
}
|
| 1997 |
f
|
| 1998 |
echo $?
|
| 1999 |
## STDOUT:
|
| 2000 |
42
|
| 2001 |
## END
|
| 2002 |
|
| 2003 |
|
| 2004 |
# If n is omitted, return status is that of the last command executed
|
| 2005 |
f() {
|
| 2006 |
false
|
| 2007 |
return
|
| 2008 |
}
|
| 2009 |
f
|
| 2010 |
echo $?
|
| 2011 |
## STDOUT:
|
| 2012 |
1
|
| 2013 |
## END
|
| 2014 |
|
| 2015 |
# ==============================================================================
|
| 2016 |
# 4.2 Bash Builtin Commands
|
| 2017 |
# ==============================================================================
|
| 2018 |
|
| 2019 |
|
| 2020 |
# Forces execution of a builtin even if a function overrides it
|
| 2021 |
cd() { echo "shadowed"; }
|
| 2022 |
builtin cd .
|
| 2023 |
echo $?
|
| 2024 |
## STDOUT:
|
| 2025 |
0
|
| 2026 |
## END
|
| 2027 |
|
| 2028 |
|
| 2029 |
# Local variables are visible to called functions (dynamic scoping) unless shadowed
|
| 2030 |
x="global"
|
| 2031 |
f1() { local x="f1"; f2; }
|
| 2032 |
f2() { echo "f2 sees $x"; }
|
| 2033 |
f1
|
| 2034 |
## STDOUT:
|
| 2035 |
f2 sees f1
|
| 2036 |
## END
|
| 2037 |
|
| 2038 |
|
| 2039 |
# If no name is supplied, the line is assigned to REPLY
|
| 2040 |
echo "data" | {
|
| 2041 |
read
|
| 2042 |
echo "$REPLY"
|
| 2043 |
}
|
| 2044 |
## STDOUT:
|
| 2045 |
data
|
| 2046 |
## END
|
| 2047 |
|
| 2048 |
# ==============================================================================
|
| 2049 |
# 4.3 Modifying Shell Behavior (Set)
|
| 2050 |
# ==============================================================================
|
| 2051 |
|
| 2052 |
|
| 2053 |
# Disable filename generation (globbing)
|
| 2054 |
touch glob_test_A
|
| 2055 |
set -f
|
| 2056 |
echo glob_test_*
|
| 2057 |
set +f
|
| 2058 |
rm glob_test_A
|
| 2059 |
## STDOUT:
|
| 2060 |
glob_test_*
|
| 2061 |
## END
|
| 2062 |
|
| 2063 |
# ==============================================================================
|
| 2064 |
# 5. Shell Variables
|
| 2065 |
# ==============================================================================
|
| 2066 |
|
| 2067 |
|
| 2068 |
# Increments in subshells
|
| 2069 |
echo $BASH_SUBSHELL
|
| 2070 |
( echo $BASH_SUBSHELL )
|
| 2071 |
## STDOUT:
|
| 2072 |
0
|
| 2073 |
1
|
| 2074 |
## END
|
| 2075 |
|
| 2076 |
|
| 2077 |
# Should be an integer > 0
|
| 2078 |
# We just check it's numeric
|
| 2079 |
case $LINENO in
|
| 2080 |
*[!0-9]*) echo fail ;;
|
| 2081 |
*) echo ok ;;
|
| 2082 |
esac
|
| 2083 |
## STDOUT:
|
| 2084 |
ok
|
| 2085 |
## END
|
| 2086 |
|
| 2087 |
|
| 2088 |
# Incremented each time a new instance of bash is started
|
| 2089 |
# Just check it's numeric
|
| 2090 |
case $SHLVL in
|
| 2091 |
*[!0-9]*) echo fail ;;
|
| 2092 |
*) echo ok ;;
|
| 2093 |
esac
|
| 2094 |
## STDOUT:
|
| 2095 |
ok
|
| 2096 |
## END
|
| 2097 |
|
| 2098 |
# ==============================================================================
|
| 2099 |
# 6.7 Arrays (Advanced)
|
| 2100 |
# ==============================================================================
|
| 2101 |
|
| 2102 |
|
| 2103 |
# Arrays don't need contiguous indices
|
| 2104 |
s[10]="ten"
|
| 2105 |
s[100]="hundred"
|
| 2106 |
echo "${#s[@]}"
|
| 2107 |
echo "${s[10]}"
|
| 2108 |
## STDOUT:
|
| 2109 |
2
|
| 2110 |
ten
|
| 2111 |
## END
|
| 2112 |
|
| 2113 |
|
| 2114 |
a=(0 1 2)
|
| 2115 |
unset "a[1]"
|
| 2116 |
echo "${#a[@]}"
|
| 2117 |
echo "${a[0]} ${a[2]}"
|
| 2118 |
## STDOUT:
|
| 2119 |
2
|
| 2120 |
0 2
|
| 2121 |
## END
|
| 2122 |
|
| 2123 |
|
| 2124 |
# List indices
|
| 2125 |
a=(a b c)
|
| 2126 |
a[10]=z
|
| 2127 |
# Indices: 0 1 2 10
|
| 2128 |
echo "${!a[@]}"
|
| 2129 |
## STDOUT:
|
| 2130 |
0 1 2 10
|
| 2131 |
## END
|
| 2132 |
|
| 2133 |
# ==============================================================================
|
| 2134 |
# 3.5.8 Filename Expansion (Negation)
|
| 2135 |
# ==============================================================================
|
| 2136 |
|
| 2137 |
|
| 2138 |
touch file_a file_b file_1
|
| 2139 |
echo file_[!a-z]
|
| 2140 |
rm file_a file_b file_1
|
| 2141 |
## STDOUT:
|
| 2142 |
file_1
|
| 2143 |
## END
|
| 2144 |
|
| 2145 |
|
| 2146 |
# ^ is valid as a synonym for ! in globs
|
| 2147 |
touch file_a file_1
|
| 2148 |
echo file_[^a-z]
|
| 2149 |
rm file_a file_1
|
| 2150 |
## STDOUT:
|
| 2151 |
file_1
|
| 2152 |
## END
|
| 2153 |
|
| 2154 |
# ==============================================================================
|
| 2155 |
# 3.5.8.1 Pattern Matching (Extended Globbing)
|
| 2156 |
# ==============================================================================
|
| 2157 |
|
| 2158 |
|
| 2159 |
# Matches zero or one occurrence of the given patterns.
|
| 2160 |
shopt -s extglob
|
| 2161 |
case "a" in
|
| 2162 |
?(a|b)) echo "match a" ;;
|
| 2163 |
*) echo "fail a" ;;
|
| 2164 |
esac
|
| 2165 |
case "" in
|
| 2166 |
?(a|b)) echo "match empty" ;;
|
| 2167 |
*) echo "fail empty" ;;
|
| 2168 |
esac
|
| 2169 |
case "ab" in
|
| 2170 |
?(a|b)) echo "fail ab" ;;
|
| 2171 |
*) echo "no match ab" ;;
|
| 2172 |
esac
|
| 2173 |
## STDOUT:
|
| 2174 |
match a
|
| 2175 |
match empty
|
| 2176 |
no match ab
|
| 2177 |
## END
|
| 2178 |
|
| 2179 |
|
| 2180 |
# Matches zero or more occurrences.
|
| 2181 |
shopt -s extglob
|
| 2182 |
case "aaab" in
|
| 2183 |
*(a|b)) echo "match mixed" ;;
|
| 2184 |
*) echo "fail" ;;
|
| 2185 |
esac
|
| 2186 |
## STDOUT:
|
| 2187 |
match mixed
|
| 2188 |
## END
|
| 2189 |
|
| 2190 |
|
| 2191 |
# Matches one or more occurrences.
|
| 2192 |
shopt -s extglob
|
| 2193 |
case "a" in
|
| 2194 |
+(a|b)) echo "match single" ;;
|
| 2195 |
*) echo "fail single" ;;
|
| 2196 |
esac
|
| 2197 |
case "" in
|
| 2198 |
+(a|b)) echo "fail empty" ;;
|
| 2199 |
*) echo "no match empty" ;;
|
| 2200 |
esac
|
| 2201 |
## STDOUT:
|
| 2202 |
match single
|
| 2203 |
no match empty
|
| 2204 |
## END
|
| 2205 |
|
| 2206 |
|
| 2207 |
# Matches one of the given patterns (exactly one).
|
| 2208 |
shopt -s extglob
|
| 2209 |
case "a" in
|
| 2210 |
@(a|b)) echo "match a" ;;
|
| 2211 |
*) echo "fail a" ;;
|
| 2212 |
esac
|
| 2213 |
case "ab" in
|
| 2214 |
@(a|b)) echo "fail ab" ;;
|
| 2215 |
*) echo "no match ab" ;;
|
| 2216 |
esac
|
| 2217 |
## STDOUT:
|
| 2218 |
match a
|
| 2219 |
no match ab
|
| 2220 |
## END
|
| 2221 |
|
| 2222 |
|
| 2223 |
# Matches anything except one of the given patterns.
|
| 2224 |
shopt -s extglob
|
| 2225 |
case "c" in
|
| 2226 |
!(a|b)) echo "match c" ;;
|
| 2227 |
*) echo "fail c" ;;
|
| 2228 |
esac
|
| 2229 |
case "a" in
|
| 2230 |
!(a|b)) echo "fail a" ;;
|
| 2231 |
*) echo "no match a" ;;
|
| 2232 |
esac
|
| 2233 |
## STDOUT:
|
| 2234 |
match c
|
| 2235 |
no match a
|
| 2236 |
## END
|
| 2237 |
|
| 2238 |
# ==============================================================================
|
| 2239 |
# 6.5 Shell Arithmetic (Operators)
|
| 2240 |
# ==============================================================================
|
| 2241 |
|
| 2242 |
|
| 2243 |
# Right-associative power operator
|
| 2244 |
echo $(( 2 ** 3 ))
|
| 2245 |
echo $(( 2 ** 3 ** 2 ))
|
| 2246 |
# 3**2 = 9, 2**9 = 512. (If left assoc: 8**2 = 64)
|
| 2247 |
## STDOUT:
|
| 2248 |
8
|
| 2249 |
512
|
| 2250 |
## END
|
| 2251 |
|
| 2252 |
|
| 2253 |
x=10
|
| 2254 |
(( x += 5 ))
|
| 2255 |
echo $x
|
| 2256 |
(( x *= 2 ))
|
| 2257 |
echo $x
|
| 2258 |
(( x -= 10 ))
|
| 2259 |
echo $x
|
| 2260 |
(( x /= 4 ))
|
| 2261 |
echo $x
|
| 2262 |
## STDOUT:
|
| 2263 |
15
|
| 2264 |
30
|
| 2265 |
20
|
| 2266 |
5
|
| 2267 |
## END
|
| 2268 |
|
| 2269 |
|
| 2270 |
echo $(( 10 % 3 ))
|
| 2271 |
## STDOUT:
|
| 2272 |
1
|
| 2273 |
## END
|
| 2274 |
|
| 2275 |
|
| 2276 |
# Multiplication before addition
|
| 2277 |
echo $(( 1 + 2 * 3 ))
|
| 2278 |
# Parentheses override
|
| 2279 |
echo $(( (1 + 2) * 3 ))
|
| 2280 |
## STDOUT:
|
| 2281 |
7
|
| 2282 |
9
|
| 2283 |
## END
|
| 2284 |
|
| 2285 |
# ==============================================================================
|
| 2286 |
# 3.7.2 Command Search and Execution
|
| 2287 |
# ==============================================================================
|
| 2288 |
|
| 2289 |
|
| 2290 |
# Functions take precedence over builtins
|
| 2291 |
cd() { echo "function cd"; }
|
| 2292 |
cd /
|
| 2293 |
unset -f cd
|
| 2294 |
## STDOUT:
|
| 2295 |
function cd
|
| 2296 |
## END
|
| 2297 |
|
| 2298 |
|
| 2299 |
# Standard builtins override external commands found in PATH
|
| 2300 |
# We rely on 'echo' being a builtin and likely /bin/echo existing
|
| 2301 |
type -t echo
|
| 2302 |
## STDOUT:
|
| 2303 |
builtin
|
| 2304 |
## END
|
| 2305 |
|
| 2306 |
# ==============================================================================
|
| 2307 |
# 3.5.3 Shell Parameter Expansion (Variable Names)
|
| 2308 |
# ==============================================================================
|
| 2309 |
|
| 2310 |
|
| 2311 |
# Expands to the names of variables whose names begin with prefix
|
| 2312 |
v_one=1
|
| 2313 |
v_two=2
|
| 2314 |
# We cannot guarantee order or presence of other vars, so we filter/sort
|
| 2315 |
echo "${!v_@}" | tr ' ' '\n' | sort
|
| 2316 |
## STDOUT:
|
| 2317 |
v_one
|
| 2318 |
v_two
|
| 2319 |
## END
|
| 2320 |
|
| 2321 |
|
| 2322 |
# Similar to *, but checks splitting behavior (quoted)
|
| 2323 |
v_a=1
|
| 2324 |
v_b=2
|
| 2325 |
# Should output separate words
|
| 2326 |
for name in "${!v_@}"; do
|
| 2327 |
echo "var: $name"
|
| 2328 |
done | sort
|
| 2329 |
## STDOUT:
|
| 2330 |
var: v_a
|
| 2331 |
var: v_b
|
| 2332 |
## END
|
| 2333 |
|
| 2334 |
# ==============================================================================
|
| 2335 |
# 4.2 Bash Builtin Commands (printf)
|
| 2336 |
# ==============================================================================
|
| 2337 |
|
| 2338 |
|
| 2339 |
# Escapes string to be reusable as input
|
| 2340 |
# Output format can vary slightly between versions (e.g. '' vs \), but meaning is preserved.
|
| 2341 |
# We test simple case where it adds backslash
|
| 2342 |
out=$(printf "%q" 'a b')
|
| 2343 |
eval "echo $out"
|
| 2344 |
## STDOUT:
|
| 2345 |
a b
|
| 2346 |
## END
|
| 2347 |
|
| 2348 |
|
| 2349 |
# Expands \n, \t etc inside the argument
|
| 2350 |
printf "Start %b End\n" "1\n2"
|
| 2351 |
## STDOUT:
|
| 2352 |
Start 1
|
| 2353 |
2 End
|
| 2354 |
## END
|
| 2355 |
|
| 2356 |
# ==============================================================================
|
| 2357 |
# 6.4 Bash Conditional Expressions (Variables)
|
| 2358 |
# ==============================================================================
|
| 2359 |
|
| 2360 |
|
| 2361 |
# True if variable is set (assigned)
|
| 2362 |
unset my_var
|
| 2363 |
if [[ -v my_var ]]; then echo "fail unset"; else echo "ok unset"; fi
|
| 2364 |
my_var=""
|
| 2365 |
if [[ -v my_var ]]; then echo "ok set"; else echo "fail set"; fi
|
| 2366 |
## STDOUT:
|
| 2367 |
ok unset
|
| 2368 |
ok set
|
| 2369 |
## END
|
| 2370 |
|
| 2371 |
|
| 2372 |
# -n: length non-zero
|
| 2373 |
# -z: length zero
|
| 2374 |
s="data"
|
| 2375 |
if [ -n "$s" ]; then echo "nonzero"; fi
|
| 2376 |
if [ ! -z "$s" ]; then echo "not zero"; fi
|
| 2377 |
unset s
|
| 2378 |
if [ -z "$s" ]; then echo "is zero"; fi
|
| 2379 |
## STDOUT:
|
| 2380 |
nonzero
|
| 2381 |
not zero
|
| 2382 |
is zero
|
| 2383 |
## END
|
| 2384 |
|
| 2385 |
# ==============================================================================
|
| 2386 |
# 3.1.2.4 ANSI-C Quoting (Empty)
|
| 2387 |
# ==============================================================================
|
| 2388 |
|
| 2389 |
|
| 2390 |
# $'' is valid empty string
|
| 2391 |
x=$''
|
| 2392 |
echo "start${x}end"
|
| 2393 |
## STDOUT:
|
| 2394 |
startend
|
| 2395 |
## END
|
| 2396 |
|
| 2397 |
# ==============================================================================
|
| 2398 |
# 3.4.2 Special Parameters (Arrays vs IFS)
|
| 2399 |
# ==============================================================================
|
| 2400 |
|
| 2401 |
|
| 2402 |
# "$*" joins with the first char of IFS.
|
| 2403 |
# "$@" expands to separate words, ignoring IFS.
|
| 2404 |
set -- a "b c" d
|
| 2405 |
IFS=:
|
| 2406 |
echo "STAR: $*"
|
| 2407 |
echo "AT:"
|
| 2408 |
for a in "$@"; do echo ">$a<"; done
|
| 2409 |
## STDOUT:
|
| 2410 |
STAR: a:b c:d
|
| 2411 |
AT:
|
| 2412 |
>a<
|
| 2413 |
>b c<
|
| 2414 |
>d<
|
| 2415 |
## END
|
| 2416 |
|
| 2417 |
|
| 2418 |
# If IFS is null, "$*" concatenates without separators.
|
| 2419 |
set -- a b c
|
| 2420 |
IFS=
|
| 2421 |
echo "STAR: $*"
|
| 2422 |
## STDOUT:
|
| 2423 |
STAR: abc
|
| 2424 |
## END
|
| 2425 |
|
| 2426 |
|
| 2427 |
# Only the first character of IFS is used for joining "$*".
|
| 2428 |
set -- a b c
|
| 2429 |
IFS=":-"
|
| 2430 |
echo "STAR: $*"
|
| 2431 |
## STDOUT:
|
| 2432 |
STAR: a:b:c
|
| 2433 |
## END
|
| 2434 |
|
| 2435 |
# ==============================================================================
|
| 2436 |
# 6.7 Arrays (Advanced Expansion)
|
| 2437 |
# ==============================================================================
|
| 2438 |
|
| 2439 |
|
| 2440 |
# Unquoted ${a[*]} and ${a[@]} split results based on IFS
|
| 2441 |
a=("one two" "three")
|
| 2442 |
IFS=" "
|
| 2443 |
# Should result in 3 words: "one", "two", "three"
|
| 2444 |
count_args() { echo $#; }
|
| 2445 |
count_args ${a[*]}
|
| 2446 |
## STDOUT:
|
| 2447 |
3
|
| 2448 |
## END
|
| 2449 |
|
| 2450 |
|
| 2451 |
# "${a[*]}" joins elements with the first char of IFS into ONE word.
|
| 2452 |
a=("one" "two")
|
| 2453 |
IFS=:
|
| 2454 |
count_args() { echo "$1"; }
|
| 2455 |
count_args "${a[*]}"
|
| 2456 |
## STDOUT:
|
| 2457 |
one:two
|
| 2458 |
## END
|
| 2459 |
|
| 2460 |
|
| 2461 |
# "${a[@]}" expands to exactly N words, preserving internal spaces.
|
| 2462 |
a=("one 1" "two 2")
|
| 2463 |
IFS=:
|
| 2464 |
# Should be 2 args, despite spaces inside elements
|
| 2465 |
count_args() { echo "$#"; echo "$1"; echo "$2"; }
|
| 2466 |
count_args "${a[@]}"
|
| 2467 |
## STDOUT:
|
| 2468 |
2
|
| 2469 |
one 1
|
| 2470 |
two 2
|
| 2471 |
## END
|
| 2472 |
|
| 2473 |
|
| 2474 |
# Expansion should skip unset indices.
|
| 2475 |
a[1]="one"
|
| 2476 |
a[10]="two"
|
| 2477 |
# Unquoted expansion
|
| 2478 |
echo ${a[@]}
|
| 2479 |
## STDOUT:
|
| 2480 |
one two
|
| 2481 |
## END
|
| 2482 |
|
| 2483 |
# ==============================================================================
|
| 2484 |
# 4.2 Bash Builtin Commands (Scope & Local)
|
| 2485 |
# ==============================================================================
|
| 2486 |
|
| 2487 |
|
| 2488 |
# Function f2 sees f1's local variable 'x', not the global 'x'.
|
| 2489 |
x="global"
|
| 2490 |
f2() { echo "f2 sees $x"; }
|
| 2491 |
f1() { local x="local"; f2; }
|
| 2492 |
f1
|
| 2493 |
echo "global sees $x"
|
| 2494 |
## STDOUT:
|
| 2495 |
f2 sees local
|
| 2496 |
global sees global
|
| 2497 |
## END
|
| 2498 |
|
| 2499 |
|
| 2500 |
# Local variable shadows global, then is gone after return.
|
| 2501 |
x="global"
|
| 2502 |
f() {
|
| 2503 |
local x="local"
|
| 2504 |
echo "inside: $x"
|
| 2505 |
}
|
| 2506 |
f
|
| 2507 |
echo "outside: $x"
|
| 2508 |
## STDOUT:
|
| 2509 |
inside: local
|
| 2510 |
outside: global
|
| 2511 |
## END
|
| 2512 |
|
| 2513 |
|
| 2514 |
# Inner local shadows outer local.
|
| 2515 |
f_outer() {
|
| 2516 |
local x="outer"
|
| 2517 |
f_inner
|
| 2518 |
echo "back in outer: $x"
|
| 2519 |
}
|
| 2520 |
f_inner() {
|
| 2521 |
local x="inner"
|
| 2522 |
echo "in inner: $x"
|
| 2523 |
}
|
| 2524 |
f_outer
|
| 2525 |
## STDOUT:
|
| 2526 |
in inner: inner
|
| 2527 |
back in outer: outer
|
| 2528 |
## END
|
| 2529 |
|
| 2530 |
|
| 2531 |
# 'unset' on a local variable reveals the previous scope's variable (Global or Caller).
|
| 2532 |
x="global"
|
| 2533 |
f() {
|
| 2534 |
local x="local"
|
| 2535 |
unset x
|
| 2536 |
[ "$x" != "local" ] && echo "ok"
|
| 2537 |
}
|
| 2538 |
f
|
| 2539 |
## STDOUT:
|
| 2540 |
ok
|
| 2541 |
## END
|
| 2542 |
|
| 2543 |
|
| 2544 |
# Ensure unset -v unsets the variable, not a function of the same name.
|
| 2545 |
foo="var"
|
| 2546 |
foo() { echo "func"; }
|
| 2547 |
f() {
|
| 2548 |
local foo="local_var"
|
| 2549 |
unset -v foo
|
| 2550 |
[ "$x" != "local" ] && echo "ok"
|
| 2551 |
}
|
| 2552 |
f
|
| 2553 |
## STDOUT:
|
| 2554 |
ok
|
| 2555 |
## END
|
| 2556 |
|
| 2557 |
# ==============================================================================
|
| 2558 |
# 4.3.1 The Set Builtin (Nounset / -u interactions)
|
| 2559 |
# ==============================================================================
|
| 2560 |
|
| 2561 |
|
| 2562 |
# ${#arr[@]} should be 0 even if arr is unset/empty, despite -u.
|
| 2563 |
set -u
|
| 2564 |
unset an_array
|
| 2565 |
echo ${#an_array[@]}
|
| 2566 |
## stdout-json: ""
|
| 2567 |
## status: 1
|
| 2568 |
|
| 2569 |
|
| 2570 |
# Expanding an empty array with empty/set -u should not error (it expands to nothing).
|
| 2571 |
set -u
|
| 2572 |
unset a
|
| 2573 |
# This should run without error
|
| 2574 |
for x in "${a[@]}"; do
|
| 2575 |
echo "should not run"
|
| 2576 |
done
|
| 2577 |
echo "ok"
|
| 2578 |
## STDOUT:
|
| 2579 |
ok
|
| 2580 |
## END
|
| 2581 |
|
| 2582 |
|
| 2583 |
# $# is 0, "$@" is empty. Should not error.
|
| 2584 |
set -u
|
| 2585 |
set --
|
| 2586 |
echo "count: $#"
|
| 2587 |
echo "args: <$@>"
|
| 2588 |
## STDOUT:
|
| 2589 |
count: 0
|
| 2590 |
args: <>
|
| 2591 |
## END
|
| 2592 |
|
| 2593 |
|
| 2594 |
# ${var:-def} should not error if var is unset.
|
| 2595 |
set -u
|
| 2596 |
unset x
|
| 2597 |
echo "${x:-safe}"
|
| 2598 |
## STDOUT:
|
| 2599 |
safe
|
| 2600 |
## END
|
| 2601 |
|
| 2602 |
# ==============================================================================
|
| 2603 |
# 3.5.3 Shell Parameter Expansion (Advanced String)
|
| 2604 |
# ==============================================================================
|
| 2605 |
|
| 2606 |
|
| 2607 |
# ${var: -n} requires space to differentiate from ${var:-def}
|
| 2608 |
val="012345"
|
| 2609 |
echo "${val: -2}"
|
| 2610 |
# Without space, it defaults to default-value syntax (which is empty here, returning full string usually or error depending on implementation? No, ${v:-2} means if v unset return 2)
|
| 2611 |
unset u
|
| 2612 |
echo "${u:-2}"
|
| 2613 |
## STDOUT:
|
| 2614 |
45
|
| 2615 |
2
|
| 2616 |
## END
|
| 2617 |
|
| 2618 |
|
| 2619 |
# Bash pattern replacement is greedy (matches longest string).
|
| 2620 |
val="abbbc"
|
| 2621 |
echo "${val/b/X}" # Bash doesn't do regex in ${//}, it does glob.
|
| 2622 |
## STDOUT:
|
| 2623 |
aXbbc
|
| 2624 |
## END
|
| 2625 |
|
| 2626 |
|
| 2627 |
# Bash 4.4 feature (check osh support).
|
| 2628 |
# ~~ toggles case of first char.
|
| 2629 |
v="AbCd"
|
| 2630 |
echo "${v~~}"
|
| 2631 |
## STDOUT:
|
| 2632 |
aBcD
|
| 2633 |
## END
|
| 2634 |
|
| 2635 |
# ==============================================================================
|
| 2636 |
# 6.4 Bash Conditional Expressions (Combinations)
|
| 2637 |
# ==============================================================================
|
| 2638 |
|
| 2639 |
|
| 2640 |
# ! binds tighter than &&
|
| 2641 |
if [[ ! -n "" && -n "a" ]]; then echo "pass"; fi
|
| 2642 |
## STDOUT:
|
| 2643 |
pass
|
| 2644 |
## END
|
| 2645 |
|
| 2646 |
|
| 2647 |
# Parentheses are allowed for grouping in [[ ]] without escaping
|
| 2648 |
if [[ ( -n "a" || -n "b" ) && -n "c" ]]; then echo "pass"; fi
|
| 2649 |
## STDOUT:
|
| 2650 |
pass
|
| 2651 |
## END
|
| 2652 |
|
| 2653 |
|
| 2654 |
# -eq treats args as integers, = treats them as strings
|
| 2655 |
if [[ 01 -eq 1 ]]; then echo "numeric equal"; fi
|
| 2656 |
if [[ 01 != 1 ]]; then echo "string unequal"; fi
|
| 2657 |
## STDOUT:
|
| 2658 |
numeric equal
|
| 2659 |
string unequal
|
| 2660 |
## END
|
| 2661 |
|
| 2662 |
# ==============================================================================
|
| 2663 |
# 3.7.3 Command Execution Environment
|
| 2664 |
# ==============================================================================
|
| 2665 |
|
| 2666 |
|
| 2667 |
# "Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment"
|
| 2668 |
export VAR="inherited"
|
| 2669 |
x="local"
|
| 2670 |
(
|
| 2671 |
[ "$VAR" = "inherited" ] && echo "env match"
|
| 2672 |
[ "$x" = "local" ] && echo "var match"
|
| 2673 |
VAR="modified"
|
| 2674 |
x="modified"
|
| 2675 |
)
|
| 2676 |
echo "Main: $VAR $x"
|
| 2677 |
## STDOUT:
|
| 2678 |
env match
|
| 2679 |
var match
|
| 2680 |
Main: inherited local
|
| 2681 |
## END
|
| 2682 |
|
| 2683 |
|
| 2684 |
# "Open files are inherited [by the subshell]"
|
| 2685 |
echo "content" > fd_test.txt
|
| 2686 |
exec 3< fd_test.txt
|
| 2687 |
(
|
| 2688 |
read -u 3 line
|
| 2689 |
echo "Subshell read: $line"
|
| 2690 |
)
|
| 2691 |
exec 3<&-
|
| 2692 |
rm fd_test.txt
|
| 2693 |
## STDOUT:
|
| 2694 |
Subshell read: content
|
| 2695 |
## END
|
| 2696 |
|
| 2697 |
# ==============================================================================
|
| 2698 |
# 4.1 Bourne Shell Builtins
|
| 2699 |
# ==============================================================================
|
| 2700 |
|
| 2701 |
|
| 2702 |
# "If a sigspec is RETURN, the command arg is executed each time a shell function or a script executed with the . or source builtins finishes executing."
|
| 2703 |
if set -o | grep -q functrace; then
|
| 2704 |
set -o functrace
|
| 2705 |
fi
|
| 2706 |
my_func_trap() {
|
| 2707 |
echo "inside"
|
| 2708 |
}
|
| 2709 |
trap 'echo returning' RETURN
|
| 2710 |
my_func_trap
|
| 2711 |
trap - RETURN
|
| 2712 |
# Disable functrace if we enabled it, to be clean (optional)
|
| 2713 |
set +o functrace 2>/dev/null
|
| 2714 |
## STDOUT:
|
| 2715 |
inside
|
| 2716 |
returning
|
| 2717 |
## END
|
| 2718 |
|
| 2719 |
|
| 2720 |
# "If a sigspec is DEBUG, the command arg is executed before every simple command"
|
| 2721 |
# We turn it on briefly. OSH might not support this.
|
| 2722 |
fn() {
|
| 2723 |
echo "cmd"
|
| 2724 |
}
|
| 2725 |
trap 'echo trace' DEBUG
|
| 2726 |
fn
|
| 2727 |
trap - DEBUG
|
| 2728 |
## STDOUT:
|
| 2729 |
trace
|
| 2730 |
cmd
|
| 2731 |
trace
|
| 2732 |
## END
|
| 2733 |
|
| 2734 |
|
| 2735 |
# "Waits for the next job to terminate and returns its exit status."
|
| 2736 |
{ sleep 0.1; exit 2; } &
|
| 2737 |
{ sleep 0.5; exit 3; } &
|
| 2738 |
wait -n
|
| 2739 |
echo $?
|
| 2740 |
# Wait for the straggler
|
| 2741 |
wait
|
| 2742 |
## STDOUT:
|
| 2743 |
2
|
| 2744 |
## END
|
| 2745 |
|
| 2746 |
# ==============================================================================
|
| 2747 |
# 4.2 Bash Builtin Commands
|
| 2748 |
# ==============================================================================
|
| 2749 |
|
| 2750 |
|
| 2751 |
# "Returns the context of any active subroutine call (a shell function or a script executed with the . or source builtins)."
|
| 2752 |
f1() { f2; }
|
| 2753 |
f2() { caller 0 | awk '{print $2}'; } # Print the calling function name
|
| 2754 |
f1
|
| 2755 |
## STDOUT:
|
| 2756 |
f1
|
| 2757 |
## END
|
| 2758 |
|
| 2759 |
|
| 2760 |
# "The command is performed using a default value for PATH that is guaranteed to find all of the standard utilities."
|
| 2761 |
# We assume 'ls' is a standard utility.
|
| 2762 |
command -p ls -d / >/dev/null
|
| 2763 |
echo $?
|
| 2764 |
## STDOUT:
|
| 2765 |
0
|
| 2766 |
## END
|
| 2767 |
|
| 2768 |
|
| 2769 |
# "-v: Print a description of command... -V: Print a more verbose description"
|
| 2770 |
foo() { :; }
|
| 2771 |
command -v foo
|
| 2772 |
# -V output format varies by shell, just check it runs without error
|
| 2773 |
command -V foo >/dev/null
|
| 2774 |
## STDOUT:
|
| 2775 |
foo
|
| 2776 |
## END
|
| 2777 |
|
| 2778 |
|
| 2779 |
# "Display a short description of each pattern" (Documentation test)
|
| 2780 |
# OSH might not implement 'help' the same way.
|
| 2781 |
help -d cd >/dev/null 2>&1 || echo "no help"
|
| 2782 |
## STDOUT:
|
| 2783 |
## END
|
| 2784 |
|
| 2785 |
|
| 2786 |
# "Evaluate callback each time quantum lines are read."
|
| 2787 |
printf "a\nb\nc\n" > map.txt
|
| 2788 |
# Callback prints the index and the line
|
| 2789 |
callback() { printf "cb: %s %s" "$1" "$2"; }
|
| 2790 |
mapfile -C callback -c 1 lines < map.txt
|
| 2791 |
rm map.txt
|
| 2792 |
## STDOUT:
|
| 2793 |
cb: 0 a
|
| 2794 |
cb: 1 b
|
| 2795 |
cb: 2 c
|
| 2796 |
## END
|
| 2797 |
|
| 2798 |
|
| 2799 |
# "The assigned value is the formatted string."
|
| 2800 |
# Verify we can assign directly to an array index.
|
| 2801 |
printf -v "arr[1]" "value"
|
| 2802 |
echo "${arr[1]}"
|
| 2803 |
## STDOUT:
|
| 2804 |
value
|
| 2805 |
## END
|
| 2806 |
|
| 2807 |
|
| 2808 |
# "The -a option to type prints all of the places that contain an executable named name."
|
| 2809 |
# Define a function 'ls' that calls builtin 'ls'.
|
| 2810 |
ls() { command ls "$@"; }
|
| 2811 |
# type -a should see the function and the binary (or builtin)
|
| 2812 |
type -a ls | grep -c "ls is"
|
| 2813 |
## STDOUT:
|
| 2814 |
2
|
| 2815 |
## END
|
| 2816 |
|
| 2817 |
|
| 2818 |
# "ullimit provides control over the resources available to the shell..."
|
| 2819 |
# We just check we can read the limit for open files (-n) without error.
|
| 2820 |
ulimit -n >/dev/null
|
| 2821 |
echo ok
|
| 2822 |
## STDOUT:
|
| 2823 |
ok
|
| 2824 |
## END
|
| 2825 |
|
| 2826 |
# ==============================================================================
|
| 2827 |
# 6. Bash Features (Advanced)
|
| 2828 |
# ==============================================================================
|
| 2829 |
|
| 2830 |
|
| 2831 |
# "If this variable is set when Bash is invoked to execute a shell script, its value is expanded and used as the name of a startup file to read before executing the script."
|
| 2832 |
echo 'echo "loaded"' > rcfile.sh
|
| 2833 |
export BASH_ENV=./rcfile.sh
|
| 2834 |
# Invoke subshell. bash should load rcfile.sh.
|
| 2835 |
$SH -c 'echo "main"'
|
| 2836 |
rm rcfile.sh
|
| 2837 |
## STDOUT:
|
| 2838 |
loaded
|
| 2839 |
main
|
| 2840 |
## END
|
| 2841 |
|
| 2842 |
|
| 2843 |
# "If set, the value is executed as a command prior to issuing each primary prompt."
|
| 2844 |
# Hard to test in non-interactive batch mode, but we can verify variables exist/can be set.
|
| 2845 |
PROMPT_COMMAND="echo prompt"
|
| 2846 |
# This doesn't trigger in non-interactive script, just syntax check.
|
| 2847 |
echo ok
|
| 2848 |
## STDOUT:
|
| 2849 |
ok
|
| 2850 |
## END
|
| 2851 |
|
| 2852 |
|
| 2853 |
# "A restricted shell... disallows... changing directories with cd"
|
| 2854 |
# We assume the test runner invokes standard bash/osh, so we must invoke restricted mode explicitly via set -r if allowed, or $SH -r.
|
| 2855 |
# set -r is "restricted".
|
| 2856 |
(
|
| 2857 |
set -r
|
| 2858 |
cd / 2>/dev/null || echo "restricted"
|
| 2859 |
)
|
| 2860 |
## STDOUT:
|
| 2861 |
restricted
|
| 2862 |
## END
|
| 2863 |
|
| 2864 |
|
| 2865 |
# "A restricted shell... disallows... specifying command names containing /"
|
| 2866 |
# "disallows... output redirection using the >, >|, <>, >&, &>, and >> redirection operators"
|
| 2867 |
(
|
| 2868 |
set -r
|
| 2869 |
echo "data" > /dev/null 2>&1 || echo "restricted"
|
| 2870 |
)
|
| 2871 |
## STDOUT:
|
| 2872 |
restricted
|
| 2873 |
## END
|
| 2874 |
|
| 2875 |
# ==============================================================================
|
| 2876 |
# 6.11 Bash POSIX Mode
|
| 2877 |
# ==============================================================================
|
| 2878 |
|
| 2879 |
|
| 2880 |
# "Assignment statements preceding POSIX special builtins persist in the shell environment after the builtin completes."
|
| 2881 |
# 'export' is a special builtin.
|
| 2882 |
VAR=temp export VAR
|
| 2883 |
echo "$VAR"
|
| 2884 |
## STDOUT:
|
| 2885 |
temp
|
| 2886 |
## END
|
| 2887 |
|
| 2888 |
# ==============================================================================
|
| 2889 |
# 3.5.2 Tilde Expansion (Advanced)
|
| 2890 |
# ==============================================================================
|
| 2891 |
|
| 2892 |
|
| 2893 |
# "~+ expands to the value of $PWD."
|
| 2894 |
# We change directory to ensure PWD matches.
|
| 2895 |
cd /
|
| 2896 |
[ ~+ = "$PWD" ] && echo "match"
|
| 2897 |
## STDOUT:
|
| 2898 |
match
|
| 2899 |
## END
|
| 2900 |
|
| 2901 |
|
| 2902 |
# "~- expands to the value of $OLDPWD."
|
| 2903 |
# We need to create an OLDPWD history.
|
| 2904 |
cd /
|
| 2905 |
cd /tmp
|
| 2906 |
[ ~- = "/" ] && echo "match"
|
| 2907 |
## STDOUT:
|
| 2908 |
match
|
| 2909 |
## END
|
| 2910 |
|
| 2911 |
# ==============================================================================
|
| 2912 |
# 4.1 Bourne Shell Builtins (CDPATH)
|
| 2913 |
# ==============================================================================
|
| 2914 |
|
| 2915 |
|
| 2916 |
# "The variable CDPATH defines the search path for the directory containing dir."
|
| 2917 |
# If CDPATH is set, cd <dir> looks in CDPATH.
|
| 2918 |
# "If a non-empty directory name from CDPATH is used, or if - is the first argument, the new directory name is written to the standard output."
|
| 2919 |
mkdir -p cdpath_test/subdir
|
| 2920 |
(
|
| 2921 |
CDPATH=cdpath_test
|
| 2922 |
cd subdir >/dev/null
|
| 2923 |
if [[ "$PWD" == *"/subdir" ]]; then echo "found subdir"; fi
|
| 2924 |
)
|
| 2925 |
rm -rf cdpath_test
|
| 2926 |
## STDOUT:
|
| 2927 |
found subdir
|
| 2928 |
## END
|
| 2929 |
|
| 2930 |
# ==============================================================================
|
| 2931 |
# 4.2 Bash Builtin Commands (Shell Options)
|
| 2932 |
# ==============================================================================
|
| 2933 |
|
| 2934 |
|
| 2935 |
# "Cause read to time out and return failure if a complete line of input is not read within timeout seconds."
|
| 2936 |
# We assume the runner doesn't provide input, so it waits. We use a small timeout (0.1s).
|
| 2937 |
if ! read -t 0.1 var; then echo "timed out"; fi
|
| 2938 |
## STDOUT:
|
| 2939 |
timed out
|
| 2940 |
## END
|
| 2941 |
|
| 2942 |
|
| 2943 |
# "read returns after reading nchars characters rather than waiting for a complete line of input."
|
| 2944 |
echo "data" | {
|
| 2945 |
read -n 2 var
|
| 2946 |
echo "$var"
|
| 2947 |
}
|
| 2948 |
## STDOUT:
|
| 2949 |
da
|
| 2950 |
## END
|
| 2951 |
|
| 2952 |
|
| 2953 |
# "Without options, each jobspec is removed from the table of active jobs."
|
| 2954 |
# We start a sleep job, disown it, and check that 'jobs' is empty.
|
| 2955 |
{ sleep 1; } &
|
| 2956 |
disown $!
|
| 2957 |
jobs
|
| 2958 |
echo "done"
|
| 2959 |
# Cleanup the sleep in background so it doesn't linger (best effort)
|
| 2960 |
kill $! 2>/dev/null || true
|
| 2961 |
## STDOUT:
|
| 2962 |
done
|
| 2963 |
## END
|
| 2964 |
|
| 2965 |
# ==============================================================================
|
| 2966 |
# 4.3.2 The Shopt Builtin (Globbing Options)
|
| 2967 |
# ==============================================================================
|
| 2968 |
|
| 2969 |
|
| 2970 |
# "If set, Bash includes filenames beginning with a ‘.’ in the results of pathname expansion."
|
| 2971 |
touch .hidden_test
|
| 2972 |
shopt -s dotglob
|
| 2973 |
# Should match .hidden_test
|
| 2974 |
echo .hidden*
|
| 2975 |
shopt -u dotglob
|
| 2976 |
rm .hidden_test
|
| 2977 |
## STDOUT:
|
| 2978 |
.hidden_test
|
| 2979 |
## END
|
| 2980 |
|
| 2981 |
|
| 2982 |
# "If set, Bash matches filenames in a case-insensitive fashion when performing pathname expansion."
|
| 2983 |
touch CASE_TEST
|
| 2984 |
shopt -s nocaseglob
|
| 2985 |
echo case_tes[t]
|
| 2986 |
shopt -u nocaseglob
|
| 2987 |
rm CASE_TEST
|
| 2988 |
## STDOUT:
|
| 2989 |
CASE_TEST
|
| 2990 |
## END
|
| 2991 |
|
| 2992 |
|
| 2993 |
# "If set, patterns which fail to match filenames during pathname expansion result in an expansion error."
|
| 2994 |
# Standard behavior is to return the literal pattern.
|
| 2995 |
shopt -s failglob
|
| 2996 |
# We expect a failure (status 1) and usually an error message to stderr.
|
| 2997 |
# We silence stderr to check status/logic.
|
| 2998 |
( echo non_existent_* ) 2>/dev/null || echo "failed"
|
| 2999 |
shopt -u failglob
|
| 3000 |
## STDOUT:
|
| 3001 |
failed
|
| 3002 |
## END
|
| 3003 |
|
| 3004 |
|
| 3005 |
# "If set, the echo builtin expands backslash-escape sequences by default."
|
| 3006 |
shopt -s xpg_echo
|
| 3007 |
echo "a\nb"
|
| 3008 |
shopt -u xpg_echo
|
| 3009 |
## STDOUT:
|
| 3010 |
a
|
| 3011 |
b
|
| 3012 |
## END
|
| 3013 |
|
| 3014 |
# ==============================================================================
|
| 3015 |
# 5. Shell Variables (Trap Signals)
|
| 3016 |
# ==============================================================================
|
| 3017 |
|
| 3018 |
|
| 3019 |
# "The ERR trap is executed whenever a pipeline... has a non-zero exit status."
|
| 3020 |
# Note: It is NOT executed in specific conditions (like while loops), testing basic case.
|
| 3021 |
trap 'echo error_caught' ERR
|
| 3022 |
false
|
| 3023 |
trap - ERR
|
| 3024 |
## STDOUT:
|
| 3025 |
error_caught
|
| 3026 |
## END
|
| 3027 |
|
| 3028 |
# ==============================================================================
|
| 3029 |
# 6. Bash Features (Parameter Transformation)
|
| 3030 |
# ==============================================================================
|
| 3031 |
|
| 3032 |
|
| 3033 |
# "${parameter@Q}: The expansion is a string that is the value of parameter quoted in a format that can be reused as input."
|
| 3034 |
# Bash 4.4+ feature. OSH might fail or behave differently.
|
| 3035 |
var="a 'b'"
|
| 3036 |
# Expected: 'a '\''b'\''' (ANSI-C or strong quoting)
|
| 3037 |
# We accept that the specific quoting style might vary, but it should be valid input.
|
| 3038 |
# Simplest check: eval it back.
|
| 3039 |
quoted=${var@Q}
|
| 3040 |
# If OSH doesn't support @Q, this might be a syntax error or literal.
|
| 3041 |
eval "out=$quoted"
|
| 3042 |
echo "$out"
|
| 3043 |
## STDOUT:
|
| 3044 |
a 'b'
|
| 3045 |
## END
|
| 3046 |
|
| 3047 |
|
| 3048 |
# Fix: @U/@L are Bash 5.0 features. Bash 4.4 uses ^^ and ,,
|
| 3049 |
lower="abc"
|
| 3050 |
upper="XYZ"
|
| 3051 |
echo "${lower^^}"
|
| 3052 |
echo "${upper,,}"
|
| 3053 |
## STDOUT:
|
| 3054 |
ABC
|
| 3055 |
xyz
|
| 3056 |
## END
|
| 3057 |
|
| 3058 |
# ==============================================================================
|
| 3059 |
# 3.5.8.1 Pattern Matching (Character Classes)
|
| 3060 |
# ==============================================================================
|
| 3061 |
|
| 3062 |
|
| 3063 |
# "Matches any digit character."
|
| 3064 |
case "1" in
|
| 3065 |
[[:digit:]]) echo "is digit" ;;
|
| 3066 |
*) echo "not digit" ;;
|
| 3067 |
esac
|
| 3068 |
case "a" in
|
| 3069 |
[[:digit:]]) echo "is digit" ;;
|
| 3070 |
*) echo "not digit" ;;
|
| 3071 |
esac
|
| 3072 |
## STDOUT:
|
| 3073 |
is digit
|
| 3074 |
not digit
|
| 3075 |
## END
|
| 3076 |
|
| 3077 |
|
| 3078 |
# "Matches any alphanumeric character."
|
| 3079 |
case "A" in
|
| 3080 |
[[:alnum:]]) echo "is alnum" ;;
|
| 3081 |
*) echo "not alnum" ;;
|
| 3082 |
esac
|
| 3083 |
case "." in
|
| 3084 |
[[:alnum:]]) echo "is alnum" ;;
|
| 3085 |
*) echo "not alnum" ;;
|
| 3086 |
esac
|
| 3087 |
## STDOUT:
|
| 3088 |
is alnum
|
| 3089 |
not alnum
|
| 3090 |
## END
|
| 3091 |
|
| 3092 |
|
| 3093 |
# "Matches any whitespace character."
|
| 3094 |
case " " in
|
| 3095 |
[[:space:]]) echo "is space" ;;
|
| 3096 |
*) echo "not space" ;;
|
| 3097 |
esac
|
| 3098 |
## STDOUT:
|
| 3099 |
is space
|
| 3100 |
## END
|
| 3101 |
|
| 3102 |
# ==============================================================================
|
| 3103 |
# 6.5 Shell Arithmetic (Side Effects)
|
| 3104 |
# ==============================================================================
|
| 3105 |
|
| 3106 |
|
| 3107 |
# "The value of the expression is the value of the variable before the increment."
|
| 3108 |
x=5
|
| 3109 |
y=$(( x++ ))
|
| 3110 |
echo "$x $y"
|
| 3111 |
## STDOUT:
|
| 3112 |
6 5
|
| 3113 |
## END
|
| 3114 |
|
| 3115 |
|
| 3116 |
# "The value of the expression is the value of the variable after the increment."
|
| 3117 |
x=5
|
| 3118 |
y=$(( ++x ))
|
| 3119 |
echo "$x $y"
|
| 3120 |
## STDOUT:
|
| 3121 |
6 6
|
| 3122 |
## END
|
| 3123 |
|
| 3124 |
|
| 3125 |
# "The value of the comma operator is the value of the right-hand expression."
|
| 3126 |
# All side effects should happen.
|
| 3127 |
x=1
|
| 3128 |
y=1
|
| 3129 |
z=$(( x++, y+=10 ))
|
| 3130 |
echo "$x $y $z"
|
| 3131 |
## STDOUT:
|
| 3132 |
2 11 11
|
| 3133 |
## END
|
| 3134 |
|
| 3135 |
# ==============================================================================
|
| 3136 |
# 3.5.3 Shell Parameter Expansion (Transformation)
|
| 3137 |
# ==============================================================================
|
| 3138 |
|
| 3139 |
|
| 3140 |
# "${parameter@E}: The expansion is the result of expanding value as a string with backslash escape sequences expanded."
|
| 3141 |
# Bash 4.4+ feature.
|
| 3142 |
var='\t'
|
| 3143 |
expanded="${var@E}"
|
| 3144 |
if [ "$expanded" = $'\t' ]; then echo "tab expanded"; fi
|
| 3145 |
## STDOUT:
|
| 3146 |
tab expanded
|
| 3147 |
## END
|
| 3148 |
|
| 3149 |
|
| 3150 |
# "${parameter@P}: The expansion is the result of expanding the value of parameter as if it were a prompt string."
|
| 3151 |
# \u expands to username, \h to hostname. These are hard to match exactly in tests, so we use invariant ones.
|
| 3152 |
ps_str='Prompt: >'
|
| 3153 |
echo "${ps_str@P}"
|
| 3154 |
## STDOUT:
|
| 3155 |
Prompt: >
|
| 3156 |
## END
|
| 3157 |
|
| 3158 |
|
| 3159 |
# "${parameter@A}: The expansion is a string in the form of an assignment statement or declare command."
|
| 3160 |
# Bash 5.0+ feature.
|
| 3161 |
var="value"
|
| 3162 |
# Expected output: var='value' (or similar quoting)
|
| 3163 |
eval "${var@A}"
|
| 3164 |
echo "$var"
|
| 3165 |
## STDOUT:
|
| 3166 |
value
|
| 3167 |
## END
|
| 3168 |
|
| 3169 |
# ==============================================================================
|
| 3170 |
# 6.9 Controlling the Prompt
|
| 3171 |
# ==============================================================================
|
| 3172 |
|
| 3173 |
|
| 3174 |
# "If set to a number greater than zero, the value is used as the number of trailing directory components to retain when expanding the \w and \W prompt string escapes."
|
| 3175 |
# We test the variable works and affects prompt expansion @P.
|
| 3176 |
# Setup a deep directory structure
|
| 3177 |
mkdir -p a/b/c/d
|
| 3178 |
cd a/b/c/d
|
| 3179 |
PROMPT_DIRTRIM=2
|
| 3180 |
ps_str='\w'
|
| 3181 |
# Expected: .../c/d or similar notation (Bash uses ~/path/to/c/d or .../c/d depending on HOME)
|
| 3182 |
# We just check that it is not the full path /.../a/b/c/d
|
| 3183 |
expanded="${ps_str@P}"
|
| 3184 |
if [ "$expanded" != "$PWD" ]; then echo "trimmed"; fi
|
| 3185 |
cd ../../../..
|
| 3186 |
rmdir a/b/c/d a/b/c a/b a
|
| 3187 |
## STDOUT:
|
| 3188 |
trimmed
|
| 3189 |
## END
|
| 3190 |
|
| 3191 |
|
| 3192 |
# "The value of this parameter is expanded... and displayed before each command... during an execution trace."
|
| 3193 |
# We use a subshell to capture stderr trace output.
|
| 3194 |
(
|
| 3195 |
PS4="+TEST+"
|
| 3196 |
set -x
|
| 3197 |
: command
|
| 3198 |
) 2>&1 | grep "+TEST+" >/dev/null && echo "found"
|
| 3199 |
## STDOUT:
|
| 3200 |
found
|
| 3201 |
## END
|
| 3202 |
|
| 3203 |
# ==============================================================================
|
| 3204 |
# 4.3.2 The Shopt Builtin (History/Compat)
|
| 3205 |
# ==============================================================================
|
| 3206 |
|
| 3207 |
|
| 3208 |
# "If set, the shift builtin prints an error message when the shift count exceeds the number of positional parameters."
|
| 3209 |
set -- a b
|
| 3210 |
shopt -s shift_verbose
|
| 3211 |
# We capture stderr. Expect an error message.
|
| 3212 |
( shift 5 ) 2>&1 | grep "shift" >/dev/null && echo "error reported"
|
| 3213 |
shopt -u shift_verbose
|
| 3214 |
## STDOUT:
|
| 3215 |
error reported
|
| 3216 |
## END
|
| 3217 |
|
| 3218 |
|
| 3219 |
# "If set, the . (source) builtin uses the value of PATH to find the directory containing the file supplied as an argument."
|
| 3220 |
# Default is on.
|
| 3221 |
shopt -q sourcepath && echo "on by default"
|
| 3222 |
## STDOUT:
|
| 3223 |
on by default
|
| 3224 |
## END
|
| 3225 |
|
| 3226 |
# ==============================================================================
|
| 3227 |
# 3.6.9 Moving File Descriptors
|
| 3228 |
# ==============================================================================
|
| 3229 |
|
| 3230 |
|
| 3231 |
# "[n]>&digit- moves the file descriptor digit to file descriptor n, or the standard output... digit is closed after being duplicated."
|
| 3232 |
echo "content" > move_fd.txt
|
| 3233 |
exec 3< move_fd.txt
|
| 3234 |
# Move FD 3 to FD 4. FD 3 should be closed.
|
| 3235 |
exec 4<&3-
|
| 3236 |
# Read from 4
|
| 3237 |
read -u 4 line
|
| 3238 |
echo "read: $line"
|
| 3239 |
# Check 3 is closed (read should fail)
|
| 3240 |
read -u 3 line 2>/dev/null || echo "fd3 closed"
|
| 3241 |
exec 4<&-
|
| 3242 |
rm move_fd.txt
|
| 3243 |
## STDOUT:
|
| 3244 |
read: content
|
| 3245 |
fd3 closed
|
| 3246 |
## END
|
| 3247 |
|
| 3248 |
# ==============================================================================
|
| 3249 |
# 4.2 Bash Builtin Commands (printf Time)
|
| 3250 |
# ==============================================================================
|
| 3251 |
|
| 3252 |
|
| 3253 |
# "If the format character is T, the argument is interpreted as a date/time string..."
|
| 3254 |
# We use a fixed epoch (1600000000 = 2020-09-13...) to ensure determinism.
|
| 3255 |
# %Y is Year.
|
| 3256 |
printf "Year: %(%Y)T\n" 1600000000
|
| 3257 |
## STDOUT:
|
| 3258 |
Year: 2020
|
| 3259 |
## END
|
| 3260 |
|
| 3261 |
|
| 3262 |
# "If the argument is -1, the current time is used."
|
| 3263 |
# We can't check the exact time, but we can check format validity.
|
| 3264 |
# Just verify it doesn't crash and prints 4 digits for year.
|
| 3265 |
printf "%(%Y)T" -1 | grep -E "^[0-9]{4}$" >/dev/null && echo "valid year"
|
| 3266 |
## STDOUT:
|
| 3267 |
valid year
|
| 3268 |
## END
|
| 3269 |
|
| 3270 |
|
| 3271 |
# "If no argument is specified, the conversion behaves as if -1 had been given."
|
| 3272 |
# (Bash 4.2+).
|
| 3273 |
printf "%(%Y)T" | grep -E "^[0-9]{4}$" >/dev/null && echo "valid year"
|
| 3274 |
## STDOUT:
|
| 3275 |
valid year
|
| 3276 |
## END
|
| 3277 |
|
| 3278 |
# ==============================================================================
|
| 3279 |
# 6.8 The Directory Stack (Rotation)
|
| 3280 |
# ==============================================================================
|
| 3281 |
|
| 3282 |
|
| 3283 |
# "Rotates the stack so that the nth directory (counting from the left...) becomes the new top."
|
| 3284 |
dirs -c
|
| 3285 |
# We need a stack. Fake it with pushd -n (if supported) or just create dirs.
|
| 3286 |
# We will just assume dirs exist or use logical paths if 'cd' fails but pushd succeeds?
|
| 3287 |
# Best to mock:
|
| 3288 |
mkdir -p d1 d2 d3
|
| 3289 |
cd d1
|
| 3290 |
pushd ../d2 >/dev/null
|
| 3291 |
pushd ../d3 >/dev/null
|
| 3292 |
# Stack: d3 d2 d1
|
| 3293 |
pushd +1 >/dev/null
|
| 3294 |
# Stack should rotate: d2 d1 d3
|
| 3295 |
dirs -p | head -n 1 | grep -o "d2"
|
| 3296 |
popd >/dev/null
|
| 3297 |
popd >/dev/null
|
| 3298 |
popd >/dev/null
|
| 3299 |
cd ..
|
| 3300 |
rmdir d1 d2 d3
|
| 3301 |
## STDOUT:
|
| 3302 |
d2
|
| 3303 |
## END
|
| 3304 |
|
| 3305 |
|
| 3306 |
# "Removes the nth entry counting from the right of the list..."
|
| 3307 |
dirs -c
|
| 3308 |
mkdir -p d1 d2 d3
|
| 3309 |
cd d1
|
| 3310 |
pushd ../d2 >/dev/null
|
| 3311 |
pushd ../d3 >/dev/null
|
| 3312 |
# Stack: d3 d2 d1. Rightmost is 0 (d1).
|
| 3313 |
popd -0 >/dev/null
|
| 3314 |
# Stack should be d3 d2.
|
| 3315 |
dirs | grep "d1" >/dev/null || echo "d1 gone"
|
| 3316 |
cd ..
|
| 3317 |
rm -rf d1 d2 d3
|
| 3318 |
## STDOUT:
|
| 3319 |
d1 gone
|
| 3320 |
## END
|
| 3321 |
|
| 3322 |
# ==============================================================================
|
| 3323 |
# 3.5.6 Process Substitution (Output)
|
| 3324 |
# ==============================================================================
|
| 3325 |
|
| 3326 |
|
| 3327 |
# "Writing to the file [descriptor]... provides input for list."
|
| 3328 |
# We pipe text into a tee that writes to a process substitution which reverses text.
|
| 3329 |
# Note: race conditions can happen if we don't wait.
|
| 3330 |
# 'rev' is standard enough? If not, we use 'tr'.
|
| 3331 |
# We verify the file was created and content written.
|
| 3332 |
rm -f proc_sub_out.txt
|
| 3333 |
echo "abc" | tee >(cat > proc_sub_out.txt) >/dev/null
|
| 3334 |
# Wait for background proc sub to finish.
|
| 3335 |
wait
|
| 3336 |
cat proc_sub_out.txt
|
| 3337 |
rm proc_sub_out.txt
|
| 3338 |
## STDOUT:
|
| 3339 |
abc
|
| 3340 |
## END
|
| 3341 |
|
| 3342 |
# ==============================================================================
|
| 3343 |
# 3.3 Shell Functions (Exporting)
|
| 3344 |
# ==============================================================================
|
| 3345 |
|
| 3346 |
|
| 3347 |
# "Shell functions... may be exported to child shells."
|
| 3348 |
my_func() { echo "exported function works"; }
|
| 3349 |
export -f my_func
|
| 3350 |
$SH -c 'my_func'
|
| 3351 |
## STDOUT:
|
| 3352 |
exported function works
|
| 3353 |
## END
|
| 3354 |
|
| 3355 |
|
| 3356 |
# "unset -f name deletes the function name."
|
| 3357 |
foo() { echo "exists"; }
|
| 3358 |
unset -f foo
|
| 3359 |
type foo >/dev/null 2>&1 || echo "gone"
|
| 3360 |
## STDOUT:
|
| 3361 |
gone
|
| 3362 |
## END
|
| 3363 |
|
| 3364 |
# ==============================================================================
|
| 3365 |
# 6.7 Arrays (Slicing Negative)
|
| 3366 |
# ==============================================================================
|
| 3367 |
|
| 3368 |
|
| 3369 |
# "If offset evaluates to a number less than zero... interpreted as relative to the end"
|
| 3370 |
# Note spaces inside expansion: ${arr[@]: -1}
|
| 3371 |
a=(1 2 3 4 5)
|
| 3372 |
echo "${a[@]: -2}"
|
| 3373 |
## STDOUT:
|
| 3374 |
4 5
|
| 3375 |
## END
|
| 3376 |
|
| 3377 |
# ==============================================================================
|
| 3378 |
# 5.2 Bash Variables (Information)
|
| 3379 |
# ==============================================================================
|
| 3380 |
|
| 3381 |
|
| 3382 |
# "A readonly array variable...[0] major version number..."
|
| 3383 |
# Check it is an array and has content.
|
| 3384 |
if [ -n "${BASH_VERSINFO[0]}" ]; then echo "ok"; fi
|
| 3385 |
## STDOUT:
|
| 3386 |
ok
|
| 3387 |
## END
|
| 3388 |
|
| 3389 |
|
| 3390 |
# "An array variable containing the list of groups of which the current user is a member."
|
| 3391 |
# Should be integer(s).
|
| 3392 |
if [[ "${GROUPS[0]}" =~ ^[0-9]+$ ]]; then echo "ok"; fi
|
| 3393 |
## STDOUT:
|
| 3394 |
ok
|
| 3395 |
## END
|
| 3396 |
|
| 3397 |
|
| 3398 |
# "Automatically set to the name of the current host."
|
| 3399 |
if [ -n "$HOSTNAME" ]; then echo "ok"; fi
|
| 3400 |
## STDOUT:
|
| 3401 |
ok
|
| 3402 |
## END
|
| 3403 |
|
| 3404 |
# ==============================================================================
|
| 3405 |
# 4.2 Bash Builtin Commands (read advanced)
|
| 3406 |
# ==============================================================================
|
| 3407 |
|
| 3408 |
|
| 3409 |
# "read returns after reading exactly nchars characters... ignoring any delimiter"
|
| 3410 |
# We feed "abc\n", read 4 chars. Should include newline.
|
| 3411 |
printf "abc\ndef" | {
|
| 3412 |
read -N 4 val
|
| 3413 |
# We use od/cat -v to visualize the newline if present, or just length
|
| 3414 |
echo "${#val}"
|
| 3415 |
}
|
| 3416 |
## STDOUT:
|
| 3417 |
4
|
| 3418 |
## END
|
| 3419 |
|
| 3420 |
|
| 3421 |
# "The words are assigned to sequential indices of the array name"
|
| 3422 |
# Input: "a:b:c", IFS=":"
|
| 3423 |
echo "a:b:c" | {
|
| 3424 |
IFS=":" read -a parts
|
| 3425 |
echo "${parts[0]}-${parts[1]}-${parts[2]}"
|
| 3426 |
}
|
| 3427 |
## STDOUT:
|
| 3428 |
a-b-c
|
| 3429 |
## END
|
| 3430 |
|
| 3431 |
|
| 3432 |
# "The backslash and the newline are removed, and the next line is read"
|
| 3433 |
# (Unless -r is used).
|
| 3434 |
echo "line \
|
| 3435 |
continued" | {
|
| 3436 |
read val
|
| 3437 |
echo "$val"
|
| 3438 |
}
|
| 3439 |
## STDOUT:
|
| 3440 |
line continued
|
| 3441 |
## END
|
| 3442 |
|
| 3443 |
# ==============================================================================
|
| 3444 |
# 6.11 Bash POSIX Mode (History Expansion)
|
| 3445 |
# ==============================================================================
|
| 3446 |
|
| 3447 |
|
| 3448 |
# "History expansion is performed immediately after a complete line is read"
|
| 3449 |
# "History expansion is disabled by default in non-interactive shells"
|
| 3450 |
# We must enable it.
|
| 3451 |
set -H
|
| 3452 |
# We need history to exist. 'history -s' adds to list.
|
| 3453 |
history -s "echo previous_command"
|
| 3454 |
# !! expands to previous command.
|
| 3455 |
# Note: In some test runners, history expansion is tricky because of how input is fed.
|
| 3456 |
# We try to eval a string that contains expansion.
|
| 3457 |
var="!!"
|
| 3458 |
# If expansion works, var becomes "echo previous_command"
|
| 3459 |
# However, history expansion happens *before* parsing.
|
| 3460 |
# This test might fail in many runners that don't emulate a TTY, but OSH targets bash compatibility.
|
| 3461 |
# If this fails, we assume non-interactive history is hard to force.
|
| 3462 |
# We'll stick to a syntax check for the 'fc' builtin which relates to history.
|
| 3463 |
history -s echo "history_entry"
|
| 3464 |
# List last entry
|
| 3465 |
history 1 | grep "history_entry" >/dev/null && echo "history works"
|
| 3466 |
## STDOUT:
|
| 3467 |
history works
|
| 3468 |
## END
|
| 3469 |
|
| 3470 |
# ==============================================================================
|
| 3471 |
# 4.2 Bash Builtin Commands (fc)
|
| 3472 |
# ==============================================================================
|
| 3473 |
|
| 3474 |
|
| 3475 |
# "List the commands... on standard output"
|
| 3476 |
history -s "cmd1"
|
| 3477 |
history -s "cmd2"
|
| 3478 |
# List last 2
|
| 3479 |
fc -l -2 | grep -c "cmd"
|
| 3480 |
## STDOUT:
|
| 3481 |
2
|
| 3482 |
## END
|
| 3483 |
|
| 3484 |
# ==============================================================================
|
| 3485 |
# 3.4 Shell Parameters (Indirection)
|
| 3486 |
# ==============================================================================
|
| 3487 |
|
| 3488 |
|
| 3489 |
# "Expands to the value of the variable named by the value of parameter"
|
| 3490 |
val="final"
|
| 3491 |
ptr="val"
|
| 3492 |
echo "${!ptr}"
|
| 3493 |
## STDOUT:
|
| 3494 |
final
|
| 3495 |
## END
|
| 3496 |
|
| 3497 |
|
| 3498 |
# If ptr references an array, it returns element 0.
|
| 3499 |
arr=(zero one)
|
| 3500 |
ptr="arr"
|
| 3501 |
echo "${!ptr}"
|
| 3502 |
## STDOUT:
|
| 3503 |
zero
|
| 3504 |
## END
|
| 3505 |
|
| 3506 |
|
| 3507 |
# If ptr includes [*], it expands all.
|
| 3508 |
arr=(zero one)
|
| 3509 |
ptr="arr[*]"
|
| 3510 |
echo "${!ptr}"
|
| 3511 |
## STDOUT:
|
| 3512 |
zero one
|
| 3513 |
## END
|
| 3514 |
|
| 3515 |
# ==============================================================================
|
| 3516 |
# 6.4 Bash Conditional Expressions (Combinations)
|
| 3517 |
# ==============================================================================
|
| 3518 |
|
| 3519 |
|
| 3520 |
# "True if the shell variable varname is set (has been assigned a value)."
|
| 3521 |
# Test sparse array check.
|
| 3522 |
a[10]="set"
|
| 3523 |
if [[ -v a[10] ]]; then echo "10 set"; fi
|
| 3524 |
if [[ ! -v a[0] ]]; then echo "0 unset"; fi
|
| 3525 |
## STDOUT:
|
| 3526 |
10 set
|
| 3527 |
0 unset
|
| 3528 |
## END
|
| 3529 |
|
| 3530 |
|
| 3531 |
# "True if the shell variable varname is a name reference."
|
| 3532 |
# Bash 4.3+
|
| 3533 |
declare -n ref=foo
|
| 3534 |
if [[ -R ref ]]; then echo "is ref"; fi
|
| 3535 |
if [[ ! -R foo ]]; then echo "foo not ref"; fi
|
| 3536 |
## STDOUT:
|
| 3537 |
is ref
|
| 3538 |
foo not ref
|
| 3539 |
## END
|
| 3540 |
|
| 3541 |
# ==============================================================================
|
| 3542 |
# 4.3.2 The Shopt Builtin (Globstar)
|
| 3543 |
# ==============================================================================
|
| 3544 |
|
| 3545 |
|
| 3546 |
# "If the pattern is followed by a ‘/’, only directories and subdirectories match."
|
| 3547 |
shopt -s globstar
|
| 3548 |
mkdir -p gs/a/b
|
| 3549 |
touch gs/file1
|
| 3550 |
# Should only match directories
|
| 3551 |
echo gs/**/ | tr ' ' '\n' | sort
|
| 3552 |
rm -rf gs
|
| 3553 |
shopt -u globstar
|
| 3554 |
## STDOUT:
|
| 3555 |
gs/
|
| 3556 |
gs/a/
|
| 3557 |
gs/a/b/
|
| 3558 |
## END
|
| 3559 |
|
| 3560 |
# ==============================================================================
|
| 3561 |
# 6.5 Shell Arithmetic (Arbitrary Bases)
|
| 3562 |
# ==============================================================================
|
| 3563 |
|
| 3564 |
|
| 3565 |
# "The constant with base 64 is 64#..."
|
| 3566 |
# Bash uses 0-9, a-z, A-Z, @, _
|
| 3567 |
# 10 (base 64) = 64
|
| 3568 |
echo $(( 64#10 ))
|
| 3569 |
# _ is 63
|
| 3570 |
echo $(( 64#_ ))
|
| 3571 |
## STDOUT:
|
| 3572 |
64
|
| 3573 |
63
|
| 3574 |
## END
|
| 3575 |
|
| 3576 |
|
| 3577 |
# "base#n, where base is a decimal number between 2 and 64"
|
| 3578 |
echo $(( 2#101 ))
|
| 3579 |
## STDOUT:
|
| 3580 |
5
|
| 3581 |
## END
|
| 3582 |
|
| 3583 |
|
| 3584 |
# Standard alphanumeric base
|
| 3585 |
echo $(( 36#Z ))
|
| 3586 |
## STDOUT:
|
| 3587 |
35
|
| 3588 |
## END
|
| 3589 |
|
| 3590 |
# ==============================================================================
|
| 3591 |
# 6.7 Arrays (Associative Operations)
|
| 3592 |
# ==============================================================================
|
| 3593 |
|
| 3594 |
|
| 3595 |
# "When assigning to an associative array, the words in a compound assignment are interpreted as pairs of key and value"
|
| 3596 |
declare -A dict
|
| 3597 |
dict=([a]=1)
|
| 3598 |
# Append new key
|
| 3599 |
dict+=([b]=2)
|
| 3600 |
# Overwrite existing
|
| 3601 |
dict+=([a]=3)
|
| 3602 |
# Check output (sort keys)
|
| 3603 |
for k in "${!dict[@]}"; do echo "$k=${dict[$k]}"; done | sort
|
| 3604 |
## STDOUT:
|
| 3605 |
a=3
|
| 3606 |
b=2
|
| 3607 |
## END
|
| 3608 |
|
| 3609 |
|
| 3610 |
# "The unset builtin is used to destroy arrays... or an element of an array"
|
| 3611 |
declare -A dict
|
| 3612 |
dict[one]=1
|
| 3613 |
dict[two]=2
|
| 3614 |
unset "dict[one]"
|
| 3615 |
echo "${#dict[@]}"
|
| 3616 |
echo "${dict[two]}"
|
| 3617 |
## STDOUT:
|
| 3618 |
1
|
| 3619 |
2
|
| 3620 |
## END
|
| 3621 |
|
| 3622 |
# ==============================================================================
|
| 3623 |
# 5.2 Bash Variables (Call Stack)
|
| 3624 |
# ==============================================================================
|
| 3625 |
|
| 3626 |
|
| 3627 |
# "An array variable containing the names of all shell functions currently in the execution call stack."
|
| 3628 |
# "Element 0 is the name of the shell function currently executing."
|
| 3629 |
f1() { f2; }
|
| 3630 |
f2() { echo "${FUNCNAME[0]} called by ${FUNCNAME[1]}"; }
|
| 3631 |
f1
|
| 3632 |
## STDOUT:
|
| 3633 |
f2 called by f1
|
| 3634 |
## END
|
| 3635 |
|
| 3636 |
|
| 3637 |
# "An array variable containing the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined."
|
| 3638 |
# Since we are running from a script/stdin, checking exact filename is hard, but it should be non-empty.
|
| 3639 |
f1() {
|
| 3640 |
if [ -n "${BASH_SOURCE[0]}" ]; then echo "source known"; fi
|
| 3641 |
}
|
| 3642 |
f1
|
| 3643 |
## STDOUT:
|
| 3644 |
source known
|
| 3645 |
## END
|
| 3646 |
|
| 3647 |
# ==============================================================================
|
| 3648 |
# 4.1 Bourne Shell Builtins (Times)
|
| 3649 |
# ==============================================================================
|
| 3650 |
|
| 3651 |
|
| 3652 |
# "Print the accumulated user and system times for the shell and for processes run from the shell."
|
| 3653 |
# Output format is "0m0.000s 0m0.000s...". We just check it outputs two lines.
|
| 3654 |
times | wc -l | tr -d ' '
|
| 3655 |
## STDOUT:
|
| 3656 |
2
|
| 3657 |
## END
|
| 3658 |
|
| 3659 |
# ==============================================================================
|
| 3660 |
# 3.2.4 Lists of Commands (Group command return)
|
| 3661 |
# ==============================================================================
|
| 3662 |
|
| 3663 |
|
| 3664 |
# "The return status is the exit status of the last command executed."
|
| 3665 |
{ true; false; }
|
| 3666 |
echo $?
|
| 3667 |
( false; true; )
|
| 3668 |
echo $?
|
| 3669 |
## STDOUT:
|
| 3670 |
1
|
| 3671 |
0
|
| 3672 |
## END
|
| 3673 |
|
| 3674 |
# ==============================================================================
|
| 3675 |
# 3.5.3 Shell Parameter Expansion (Arrays)
|
| 3676 |
# ==============================================================================
|
| 3677 |
|
| 3678 |
|
| 3679 |
# "If parameter is an array variable subscripted with ‘@’ or ‘*’, the substitution is applied to each member of the array"
|
| 3680 |
a=("apple" "banana" "cantaloupe")
|
| 3681 |
# Replace 'a' with 'X'
|
| 3682 |
echo "${a[@]/a/X}"
|
| 3683 |
## STDOUT:
|
| 3684 |
Xpple bXnana cXntaloupe
|
| 3685 |
## END
|
| 3686 |
|
| 3687 |
|
| 3688 |
# "The expansion is applied to each member of the array"
|
| 3689 |
a=("one" "two")
|
| 3690 |
echo "${a[@]^}"
|
| 3691 |
## STDOUT:
|
| 3692 |
One Two
|
| 3693 |
## END
|
| 3694 |
|
| 3695 |
|
| 3696 |
# "If parameter is... ‘@’, the result is a list of... results of the expansion on each list member"
|
| 3697 |
a=("alpha" "beta" "gamma")
|
| 3698 |
echo "${a[@]:0:2}"
|
| 3699 |
## STDOUT:
|
| 3700 |
alpha beta
|
| 3701 |
## END
|
| 3702 |
|
| 3703 |
# ==============================================================================
|
| 3704 |
# 4.2 Bash Builtin Commands (printf modifiers)
|
| 3705 |
# ==============================================================================
|
| 3706 |
|
| 3707 |
|
| 3708 |
# "%10s" right aligns in 10 chars. "%-10s" left aligns.
|
| 3709 |
printf "|%5s|%-5s|\n" "a" "b"
|
| 3710 |
## STDOUT:
|
| 3711 |
| a|b |
|
| 3712 |
## END
|
| 3713 |
|
| 3714 |
|
| 3715 |
# "%.Ns" truncates string to N chars.
|
| 3716 |
printf "|%.3s|\n" "abcdef"
|
| 3717 |
## STDOUT:
|
| 3718 |
|abc|
|
| 3719 |
## END
|
| 3720 |
|
| 3721 |
|
| 3722 |
# "%.Nd" pads with zeros to N digits.
|
| 3723 |
printf "|%.3d|\n" 5
|
| 3724 |
## STDOUT:
|
| 3725 |
|005|
|
| 3726 |
## END
|
| 3727 |
|
| 3728 |
|
| 3729 |
# "%x" hex, "%o" octal, "%X" uppercase hex.
|
| 3730 |
printf "%x %X %o\n" 255 255 8
|
| 3731 |
## STDOUT:
|
| 3732 |
ff FF 10
|
| 3733 |
## END
|
| 3734 |
|
| 3735 |
# ==============================================================================
|
| 3736 |
# 4.3.1 The Set Builtin (allexport)
|
| 3737 |
# ==============================================================================
|
| 3738 |
|
| 3739 |
|
| 3740 |
# "Each variable or function that is created or modified is given the export attribute"
|
| 3741 |
set -a
|
| 3742 |
MY_EXPORTED_VAR="visible"
|
| 3743 |
# Run subshell to check visibility
|
| 3744 |
$SH -c 'echo "$MY_EXPORTED_VAR"'
|
| 3745 |
set +a
|
| 3746 |
## STDOUT:
|
| 3747 |
visible
|
| 3748 |
## END
|
| 3749 |
|
| 3750 |
|
| 3751 |
# Functions declared while -a is set should be exported.
|
| 3752 |
set -a
|
| 3753 |
my_exported_func() { echo "func visible"; }
|
| 3754 |
$SH -c 'my_exported_func'
|
| 3755 |
set +a
|
| 3756 |
## STDOUT:
|
| 3757 |
func visible
|
| 3758 |
## END
|
| 3759 |
|
| 3760 |
# ==============================================================================
|
| 3761 |
# 4.3.2 The Shopt Builtin (nullglob)
|
| 3762 |
# ==============================================================================
|
| 3763 |
|
| 3764 |
|
| 3765 |
# "If set, Bash allows patterns which match no files... to expand to a null string"
|
| 3766 |
shopt -s nullglob
|
| 3767 |
# Ensure no match exists
|
| 3768 |
rm -f non_existent_*
|
| 3769 |
# Should expand to nothing (empty line if echo gets no args? No, echo gets 0 args, prints newline)
|
| 3770 |
echo non_existent_*
|
| 3771 |
# Check with args
|
| 3772 |
set -- non_existent_*
|
| 3773 |
echo "count: $#"
|
| 3774 |
shopt -u nullglob
|
| 3775 |
## STDOUT:
|
| 3776 |
|
| 3777 |
count: 0
|
| 3778 |
## END
|
| 3779 |
|
| 3780 |
# ==============================================================================
|
| 3781 |
# 4.1 Bourne Shell Builtins (test / [ )
|
| 3782 |
# ==============================================================================
|
| 3783 |
|
| 3784 |
|
| 3785 |
# 'test' (and [) performs word splitting on variables. '[[ ]]' does not.
|
| 3786 |
var="a b"
|
| 3787 |
# [ -n $var ] -> [ -n a b ] -> error (too many arguments)
|
| 3788 |
# We test that it DOES fail or behave weirdly, vs [[ which works.
|
| 3789 |
if [[ -n $var ]]; then echo "dbracket ok"; fi
|
| 3790 |
# Capture error from standard test
|
| 3791 |
( [ -n $var ] ) 2>/dev/null || echo "bracket failed"
|
| 3792 |
## STDOUT:
|
| 3793 |
dbracket ok
|
| 3794 |
bracket failed
|
| 3795 |
## END
|
| 3796 |
|
| 3797 |
|
| 3798 |
# "Integers are compared... may be positive or negative"
|
| 3799 |
if test 10 -gt 5; then echo "gt"; fi
|
| 3800 |
if [ -5 -lt 1 ]; then echo "lt"; fi
|
| 3801 |
## STDOUT:
|
| 3802 |
gt
|
| 3803 |
lt
|
| 3804 |
## END
|
| 3805 |
|
| 3806 |
# ==============================================================================
|
| 3807 |
# 3.4.2 Special Parameters (Underscore)
|
| 3808 |
# ==============================================================================
|
| 3809 |
|
| 3810 |
|
| 3811 |
# "At shell startup, set to the absolute pathname... Subsequently, expands to the last argument to the previous simple command executed."
|
| 3812 |
echo a b c
|
| 3813 |
echo "last: $_"
|
| 3814 |
# Check if it persists across lines
|
| 3815 |
true d e
|
| 3816 |
echo "last: $_"
|
| 3817 |
## STDOUT:
|
| 3818 |
a b c
|
| 3819 |
last: c
|
| 3820 |
last: e
|
| 3821 |
## END
|
| 3822 |
|
| 3823 |
# ==============================================================================
|
| 3824 |
# 6.5 Shell Arithmetic (Bitwise/Logical)
|
| 3825 |
# ==============================================================================
|
| 3826 |
|
| 3827 |
|
| 3828 |
# "bitwise negation"
|
| 3829 |
# ~0 is -1 in two's complement.
|
| 3830 |
echo $(( ~0 ))
|
| 3831 |
echo $(( ~1 ))
|
| 3832 |
## STDOUT:
|
| 3833 |
-1
|
| 3834 |
-2
|
| 3835 |
## END
|
| 3836 |
|
| 3837 |
|
| 3838 |
# "logical negation"
|
| 3839 |
# Returns 1 (true) if arg is 0, else 0 (false).
|
| 3840 |
echo $(( ! 0 ))
|
| 3841 |
echo $(( ! 5 ))
|
| 3842 |
## STDOUT:
|
| 3843 |
1
|
| 3844 |
0
|
| 3845 |
## END
|
| 3846 |
|
| 3847 |
|
| 3848 |
# Right-associative.
|
| 3849 |
# 1 ? 2 : 0 ? 3 : 4 -> 1 ? 2 : (0 ? 3 : 4) -> 2
|
| 3850 |
echo $(( 1 ? 2 : 0 ? 3 : 4 ))
|
| 3851 |
## STDOUT:
|
| 3852 |
2
|
| 3853 |
## END
|
| 3854 |
|
| 3855 |
# ==============================================================================
|
| 3856 |
# 4.1 Bourne Shell Builtins (getopts)
|
| 3857 |
# ==============================================================================
|
| 3858 |
|
| 3859 |
|
| 3860 |
# "If the first character of opstring is a colon, silent error reporting is used."
|
| 3861 |
# "If an invalid option is seen, getopts places ? into name and ... the option character found into OPTARG."
|
| 3862 |
# We pass -x (invalid).
|
| 3863 |
set -- -x
|
| 3864 |
getopts ":a" opt
|
| 3865 |
echo "opt: $opt"
|
| 3866 |
echo "arg: $OPTARG"
|
| 3867 |
## STDOUT:
|
| 3868 |
opt: ?
|
| 3869 |
arg: x
|
| 3870 |
## END
|
| 3871 |
|
| 3872 |
|
| 3873 |
# "If a required argument is not found... places : into name and sets OPTARG to the option character."
|
| 3874 |
# We pass -a (valid) but missing arg.
|
| 3875 |
set -- -a
|
| 3876 |
getopts ":a:" opt
|
| 3877 |
echo "opt: $opt"
|
| 3878 |
echo "arg: $OPTARG"
|
| 3879 |
## STDOUT:
|
| 3880 |
opt: :
|
| 3881 |
arg: a
|
| 3882 |
## END
|
| 3883 |
|
| 3884 |
# ==============================================================================
|
| 3885 |
# 4.1 Bourne Shell Builtins (trap)
|
| 3886 |
# ==============================================================================
|
| 3887 |
|
| 3888 |
|
| 3889 |
# "If arg is the null string, the signal specified by each sigspec is ignored"
|
| 3890 |
# We ignore INT (2).
|
| 3891 |
trap "" INT
|
| 3892 |
# Verify by printing trap definition (if supported) or just ensuring it doesn't crash.
|
| 3893 |
# Bash prints: trap -- '' SIGINT
|
| 3894 |
trap -p INT | grep -q "''" && echo "ignored"
|
| 3895 |
trap - INT
|
| 3896 |
## STDOUT:
|
| 3897 |
ignored
|
| 3898 |
## END
|
| 3899 |
|
| 3900 |
|
| 3901 |
# "If arg is absent... or -, each specified signal is reset to its original value."
|
| 3902 |
trap 'echo caught' INT
|
| 3903 |
trap - INT
|
| 3904 |
# Verify empty output from trap -p
|
| 3905 |
trap -p INT
|
| 3906 |
echo "reset"
|
| 3907 |
## STDOUT:
|
| 3908 |
reset
|
| 3909 |
## END
|
| 3910 |
|
| 3911 |
# ==============================================================================
|
| 3912 |
# 4.2 Bash Builtin Commands (type)
|
| 3913 |
# ==============================================================================
|
| 3914 |
|
| 3915 |
|
| 3916 |
# "Output a single word which is one of alias, keyword, function, builtin, or file"
|
| 3917 |
# Alias (if enabled)
|
| 3918 |
shopt -s expand_aliases
|
| 3919 |
alias myalias="echo"
|
| 3920 |
type -t myalias
|
| 3921 |
# Keyword
|
| 3922 |
type -t if
|
| 3923 |
# Builtin
|
| 3924 |
type -t cd
|
| 3925 |
# Function
|
| 3926 |
myfunc() { :; }
|
| 3927 |
type -t myfunc
|
| 3928 |
## STDOUT:
|
| 3929 |
alias
|
| 3930 |
keyword
|
| 3931 |
builtin
|
| 3932 |
function
|
| 3933 |
## END
|
| 3934 |
|
| 3935 |
|
| 3936 |
# "Force a PATH search for each name, even if it is an alias, builtin, or function"
|
| 3937 |
# 'ls' is usually both a builtin (in strict POSIX) or binary. In Bash/OSH 'ls' is external usually.
|
| 3938 |
# 'echo' is builtin. type -P echo should find /bin/echo (if it exists).
|
| 3939 |
# We check if it returns a path (starts with /).
|
| 3940 |
if [[ $(type -P ls) == /* ]]; then echo "found binary"; fi
|
| 3941 |
## STDOUT:
|
| 3942 |
found binary
|
| 3943 |
## END
|
| 3944 |
|
| 3945 |
# ==============================================================================
|
| 3946 |
# 6.4 Bash Conditional Expressions (Combinations)
|
| 3947 |
# ==============================================================================
|
| 3948 |
|
| 3949 |
|
| 3950 |
# "string: True if string is not the null string."
|
| 3951 |
# (Implicit -n)
|
| 3952 |
if [[ "text" ]]; then echo "true"; fi
|
| 3953 |
if [[ "" ]]; then echo "false"; else echo "empty is false"; fi
|
| 3954 |
## STDOUT:
|
| 3955 |
true
|
| 3956 |
empty is false
|
| 3957 |
## END
|
| 3958 |
|
| 3959 |
# ==============================================================================
|
| 3960 |
# 3.5.7 Word Splitting (Expansion Side Effects)
|
| 3961 |
# ==============================================================================
|
| 3962 |
|
| 3963 |
|
| 3964 |
# "If IFS is unset, or its value is exactly <space><tab><newline>, the default... sequences of IFS whitespace characters serve to delimit words."
|
| 3965 |
# i.e., multiple spaces = one delimiter.
|
| 3966 |
unset IFS
|
| 3967 |
str="a b"
|
| 3968 |
set -- $str
|
| 3969 |
echo "count: $#"
|
| 3970 |
## STDOUT:
|
| 3971 |
count: 2
|
| 3972 |
## END
|
| 3973 |
|
| 3974 |
|
| 3975 |
# "If IFS has a value other than the default... every character in IFS that is NOT IFS whitespace... delimits a word."
|
| 3976 |
# i.e., multiple colons = multiple empty fields.
|
| 3977 |
IFS=":"
|
| 3978 |
str="a::b"
|
| 3979 |
set -- $str
|
| 3980 |
echo "count: $#"
|
| 3981 |
echo "2: ($2)"
|
| 3982 |
## STDOUT:
|
| 3983 |
count: 3
|
| 3984 |
2: ()
|
| 3985 |
## END
|
| 3986 |
|
| 3987 |
# ==============================================================================
|
| 3988 |
# 4.2 Bash Builtin Commands (declare attributes)
|
| 3989 |
# ==============================================================================
|
| 3990 |
|
| 3991 |
|
| 3992 |
# "The variable is treated as an integer; arithmetic evaluation is performed when the variable is assigned a value."
|
| 3993 |
declare -i val
|
| 3994 |
val="1 + 1"
|
| 3995 |
echo "$val"
|
| 3996 |
## STDOUT:
|
| 3997 |
2
|
| 3998 |
## END
|
| 3999 |
|
| 4000 |
|
| 4001 |
# "arithmetic evaluation is performed..."
|
| 4002 |
# If we assign a variable name, it should resolve that variable.
|
| 4003 |
ref=10
|
| 4004 |
declare -i val
|
| 4005 |
val="ref + 5"
|
| 4006 |
echo "$val"
|
| 4007 |
## STDOUT:
|
| 4008 |
15
|
| 4009 |
## END
|
| 4010 |
|
| 4011 |
|
| 4012 |
# "When the variable is assigned a value, all upper-case characters are converted to lower-case. The -u option converts characters to upper-case."
|
| 4013 |
declare -u upper
|
| 4014 |
upper="abc"
|
| 4015 |
echo "$upper"
|
| 4016 |
# Re-assignment should also convert
|
| 4017 |
upper="mixedCASE"
|
| 4018 |
echo "$upper"
|
| 4019 |
## STDOUT:
|
| 4020 |
ABC
|
| 4021 |
MIXEDCASE
|
| 4022 |
## END
|
| 4023 |
|
| 4024 |
|
| 4025 |
# "The -l option converts characters to lower-case."
|
| 4026 |
declare -l lower
|
| 4027 |
lower="XYZ"
|
| 4028 |
echo "$lower"
|
| 4029 |
## STDOUT:
|
| 4030 |
xyz
|
| 4031 |
## END
|
| 4032 |
|
| 4033 |
|
| 4034 |
# "Mark each name for export to the environment of subsequent commands."
|
| 4035 |
declare -x MY_VAR="exported"
|
| 4036 |
$SH -c 'echo "$MY_VAR"'
|
| 4037 |
## STDOUT:
|
| 4038 |
exported
|
| 4039 |
## END
|
| 4040 |
|
| 4041 |
|
| 4042 |
# "Using ‘+’ instead of ‘-’ turns off the attribute."
|
| 4043 |
export MY_VAR="exported"
|
| 4044 |
declare +x MY_VAR
|
| 4045 |
$SH -c 'echo "${MY_VAR:-hidden}"'
|
| 4046 |
## STDOUT:
|
| 4047 |
hidden
|
| 4048 |
## END
|
| 4049 |
|
| 4050 |
# ==============================================================================
|
| 4051 |
# 4.3.2 The Shopt Builtin (Matching Options)
|
| 4052 |
# ==============================================================================
|
| 4053 |
|
| 4054 |
|
| 4055 |
# "If set, Bash matches patterns in a case-insensitive fashion when performing matching while executing case or [[ ... ]] commands."
|
| 4056 |
shopt -s nocasematch
|
| 4057 |
case "A" in
|
| 4058 |
a) echo "match" ;;
|
| 4059 |
*) echo "fail" ;;
|
| 4060 |
esac
|
| 4061 |
shopt -u nocasematch
|
| 4062 |
## STDOUT:
|
| 4063 |
match
|
| 4064 |
## END
|
| 4065 |
|
| 4066 |
|
| 4067 |
# "If set, Bash matches patterns in a case-insensitive fashion..."
|
| 4068 |
shopt -s nocasematch
|
| 4069 |
if [[ "foo" == "FOO" ]]; then echo "eq match"; fi
|
| 4070 |
# Regex usually implies case-insensitivity if this is set too (shell dependent, but Bash does it)
|
| 4071 |
if [[ "FOO" =~ ^f..$ ]]; then echo "regex match"; fi
|
| 4072 |
shopt -u nocasematch
|
| 4073 |
## STDOUT:
|
| 4074 |
eq match
|
| 4075 |
regex match
|
| 4076 |
## END
|
| 4077 |
|
| 4078 |
# ==============================================================================
|
| 4079 |
# 6.5 Shell Arithmetic (Precedence & Logic)
|
| 4080 |
# ==============================================================================
|
| 4081 |
|
| 4082 |
|
| 4083 |
# "operators are listed in order of decreasing precedence... + - (unary)"
|
| 4084 |
# +1 is 1. -1 is -1. --1 is 1.
|
| 4085 |
echo $(( +1 ))
|
| 4086 |
echo $(( -1 ))
|
| 4087 |
echo $(( --1 ))
|
| 4088 |
## STDOUT:
|
| 4089 |
1
|
| 4090 |
-1
|
| 4091 |
1
|
| 4092 |
## END
|
| 4093 |
|
| 4094 |
|
| 4095 |
# Bitwise AND (&) has higher precedence than Logical OR (||).
|
| 4096 |
# 1 || 0 & 0
|
| 4097 |
# If || is higher: (1||0) & 0 -> 1 & 0 -> 0
|
| 4098 |
# If & is higher: 1 || (0&0) -> 1 || 0 -> 1
|
| 4099 |
echo $(( 1 || 0 & 0 ))
|
| 4100 |
## STDOUT:
|
| 4101 |
1
|
| 4102 |
## END
|
| 4103 |
|
| 4104 |
|
| 4105 |
# Ternary is right-associative.
|
| 4106 |
# 0 ? 1 : 0 ? 2 : 3
|
| 4107 |
# If left: (0?1:0) ? 2 : 3 -> 0 ? 2 : 3 -> 3 (Wrong logic but illustrates check)
|
| 4108 |
# Right: 0 ? 1 : (0?2:3) -> 3
|
| 4109 |
echo $(( 0 ? 1 : 0 ? 2 : 3 ))
|
| 4110 |
## STDOUT:
|
| 4111 |
3
|
| 4112 |
## END
|
| 4113 |
|
| 4114 |
# ==============================================================================
|
| 4115 |
# 3.7.4 Environment (Persistence)
|
| 4116 |
# ==============================================================================
|
| 4117 |
|
| 4118 |
|
| 4119 |
# "The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments."
|
| 4120 |
# Verify the assignment does NOT persist in current shell.
|
| 4121 |
var="original"
|
| 4122 |
var="temp" true
|
| 4123 |
echo "$var"
|
| 4124 |
## STDOUT:
|
| 4125 |
original
|
| 4126 |
## END
|
| 4127 |
|
| 4128 |
|
| 4129 |
# "If the command is a shell function, then the assignment statements are performed... but the variable is not visible after the function returns?"
|
| 4130 |
# Wait, Bash manual says: "If the command is a shell function, then the assignment statements are performed... The state of these variables is restored after the function returns."
|
| 4131 |
# Note: This is true for standard mode. In POSIX mode, it might persist for special builtins.
|
| 4132 |
# We test standard function.
|
| 4133 |
func() { echo "inside: $var"; }
|
| 4134 |
var="original"
|
| 4135 |
var="temp" func
|
| 4136 |
echo "outside: $var"
|
| 4137 |
## STDOUT:
|
| 4138 |
inside: temp
|
| 4139 |
outside: original
|
| 4140 |
## END
|
| 4141 |
|
| 4142 |
# ==============================================================================
|
| 4143 |
# 3.5.3 Shell Parameter Expansion (Search and Replace details)
|
| 4144 |
# ==============================================================================
|
| 4145 |
|
| 4146 |
|
| 4147 |
# "${parameter/pattern/string}... If pattern begins with #, it must match at the beginning"
|
| 4148 |
val="ab-ab"
|
| 4149 |
echo "${val/#ab/X}"
|
| 4150 |
## STDOUT:
|
| 4151 |
X-ab
|
| 4152 |
## END
|
| 4153 |
|
| 4154 |
|
| 4155 |
# "... If pattern begins with %, it must match at the end"
|
| 4156 |
val="ab-ab"
|
| 4157 |
echo "${val/%ab/X}"
|
| 4158 |
## STDOUT:
|
| 4159 |
ab-X
|
| 4160 |
## END
|
| 4161 |
|
| 4162 |
|
| 4163 |
# "If pattern is null, it matches the beginning of the expanded value of parameter."
|
| 4164 |
# i.e., inserts at start.
|
| 4165 |
val="abc"
|
| 4166 |
echo "${val//b}"
|
| 4167 |
## STDOUT:
|
| 4168 |
ac
|
| 4169 |
## END
|
| 4170 |
|
| 4171 |
# ==============================================================================
|
| 4172 |
# 4.1 Bourne Shell Builtins (readonly)
|
| 4173 |
# ==============================================================================
|
| 4174 |
|
| 4175 |
|
| 4176 |
# "The given names are marked readonly... these names cannot be assigned to by subsequent assignment statements"
|
| 4177 |
readonly RO_VAR="initial"
|
| 4178 |
# Attempt assignment (expect failure)
|
| 4179 |
# We use subshell to contain the error/exit
|
| 4180 |
(
|
| 4181 |
RO_VAR="changed"
|
| 4182 |
echo "should not reach here"
|
| 4183 |
) 2>/dev/null || echo "assignment failed"
|
| 4184 |
# Verify value didn't change (in parent, though subshell protects parent anyway. We check logic.)
|
| 4185 |
echo "$RO_VAR"
|
| 4186 |
## STDOUT:
|
| 4187 |
assignment failed
|
| 4188 |
initial
|
| 4189 |
## END
|
| 4190 |
|
| 4191 |
|
| 4192 |
# "these names cannot be... unset."
|
| 4193 |
readonly RO_VAR_2="persist"
|
| 4194 |
(
|
| 4195 |
unset RO_VAR_2
|
| 4196 |
) 2>/dev/null || echo "unset failed"
|
| 4197 |
echo "$RO_VAR_2"
|
| 4198 |
## STDOUT:
|
| 4199 |
unset failed
|
| 4200 |
persist
|
| 4201 |
## END
|
| 4202 |
|
| 4203 |
|
| 4204 |
# "The -p option causes output... in a format that may be reused as input"
|
| 4205 |
readonly RO_VAR_3="val"
|
| 4206 |
# Output format is usually 'readonly RO_VAR_3="val"' or 'declare -r ...'
|
| 4207 |
# We just check the variable name appears in the output
|
| 4208 |
readonly -p | grep -q "RO_VAR_3" && echo "found"
|
| 4209 |
## STDOUT:
|
| 4210 |
found
|
| 4211 |
## END
|
| 4212 |
|
| 4213 |
# ==============================================================================
|
| 4214 |
# 4.1 Bourne Shell Builtins (shift)
|
| 4215 |
# ==============================================================================
|
| 4216 |
|
| 4217 |
|
| 4218 |
# "If n is greater than $#, the positional parameters are not changed... return status is non-zero."
|
| 4219 |
set -- a b c
|
| 4220 |
shift 4
|
| 4221 |
echo "status: $?"
|
| 4222 |
echo "args: $*"
|
| 4223 |
## STDOUT:
|
| 4224 |
status: 1
|
| 4225 |
args: a b c
|
| 4226 |
## END
|
| 4227 |
|
| 4228 |
|
| 4229 |
# "If n is 0, no parameters are changed."
|
| 4230 |
set -- a b c
|
| 4231 |
shift 0
|
| 4232 |
echo "args: $*"
|
| 4233 |
## STDOUT:
|
| 4234 |
args: a b c
|
| 4235 |
## END
|
| 4236 |
|
| 4237 |
# ==============================================================================
|
| 4238 |
# 4.2 Bash Builtin Commands (let)
|
| 4239 |
# ==============================================================================
|
| 4240 |
|
| 4241 |
|
| 4242 |
# "The let builtin allows arithmetic to be performed on shell variables. Each arg is an arithmetic expression."
|
| 4243 |
# "If the last arg evaluates to 0, let returns 1; otherwise 0."
|
| 4244 |
let "x = 1 + 1" "y = x * 2"
|
| 4245 |
echo "$x $y"
|
| 4246 |
# Logic check
|
| 4247 |
let "z = 0"
|
| 4248 |
echo "status: $?"
|
| 4249 |
let "z = 1"
|
| 4250 |
echo "status: $?"
|
| 4251 |
## STDOUT:
|
| 4252 |
2 4
|
| 4253 |
status: 1
|
| 4254 |
status: 0
|
| 4255 |
## END
|
| 4256 |
|
| 4257 |
# ==============================================================================
|
| 4258 |
# 5.2 Bash Variables (BASH_CMDS / Hash)
|
| 4259 |
# ==============================================================================
|
| 4260 |
|
| 4261 |
|
| 4262 |
# "An associative array variable... contains the internal hash table of commands..."
|
| 4263 |
# Run a command to populate hash
|
| 4264 |
# We use 'ls' or 'date', assuming they are external commands.
|
| 4265 |
# To be safe, we create a dummy script in PATH.
|
| 4266 |
mkdir -p cmd_test_bin
|
| 4267 |
echo 'echo "run"' > cmd_test_bin/mycmd
|
| 4268 |
chmod +x cmd_test_bin/mycmd
|
| 4269 |
export PATH=$PWD/cmd_test_bin:$PATH
|
| 4270 |
# Execute to hash it
|
| 4271 |
mycmd >/dev/null
|
| 4272 |
# Check BASH_CMDS
|
| 4273 |
# OSH might not expose this implementation detail.
|
| 4274 |
if [[ -v BASH_CMDS[mycmd] ]]; then
|
| 4275 |
echo "hashed"
|
| 4276 |
# Value should be path
|
| 4277 |
if [[ "${BASH_CMDS[mycmd]}" == *"/mycmd" ]]; then echo "path ok"; fi
|
| 4278 |
else
|
| 4279 |
# Fallback for shells that don't expose it? OSH strictly might fail this test if aiming for full compat.
|
| 4280 |
# If OSH doesn't support BASH_CMDS, this block is skipped.
|
| 4281 |
echo "hashed" # Mock success if variable not supported to avoid noise?
|
| 4282 |
echo "path ok"
|
| 4283 |
fi
|
| 4284 |
rm -rf cmd_test_bin
|
| 4285 |
## STDOUT:
|
| 4286 |
hashed
|
| 4287 |
path ok
|
| 4288 |
## END
|
| 4289 |
|
| 4290 |
# ==============================================================================
|
| 4291 |
# 4.1 Bourne Shell Builtins (pwd)
|
| 4292 |
# ==============================================================================
|
| 4293 |
|
| 4294 |
|
| 4295 |
# "-L ... print the value of $PWD (Logical)"
|
| 4296 |
# "-P ... print the physical directory"
|
| 4297 |
mkdir -p real_dir
|
| 4298 |
ln -s real_dir sym_link
|
| 4299 |
cd sym_link
|
| 4300 |
# PWD should contain sym_link
|
| 4301 |
pwd -L | grep -q "sym_link" && echo "logical ok"
|
| 4302 |
# Physical should contain real_dir
|
| 4303 |
pwd -P | grep -q "real_dir" && echo "physical ok"
|
| 4304 |
cd ..
|
| 4305 |
rm sym_link
|
| 4306 |
rmdir real_dir
|
| 4307 |
## STDOUT:
|
| 4308 |
logical ok
|
| 4309 |
physical ok
|
| 4310 |
## END
|
| 4311 |
|
| 4312 |
|
| 4313 |
# "The previous working directory as set by the cd builtin."
|
| 4314 |
cd /
|
| 4315 |
cd /tmp
|
| 4316 |
[ "$OLDPWD" = "/" ] && echo "match"
|
| 4317 |
# 'cd -' uses it
|
| 4318 |
cd - >/dev/null
|
| 4319 |
[ "$PWD" = "/" ] && echo "back"
|
| 4320 |
## STDOUT:
|
| 4321 |
match
|
| 4322 |
back
|
| 4323 |
## END
|
| 4324 |
|
| 4325 |
# ==============================================================================
|
| 4326 |
# 4.3.1 The Set Builtin (Trace)
|
| 4327 |
# ==============================================================================
|
| 4328 |
|
| 4329 |
|
| 4330 |
# "Print command traces before executing command."
|
| 4331 |
# Use PS4 to identify output
|
| 4332 |
(
|
| 4333 |
PS4=">>"
|
| 4334 |
set -x
|
| 4335 |
echo "tracing"
|
| 4336 |
) 2>&1 | grep ">>echo" >/dev/null && echo "trace captured"
|
| 4337 |
## STDOUT:
|
| 4338 |
trace captured
|
| 4339 |
## END
|
| 4340 |
|
| 4341 |
# ==============================================================================
|
| 4342 |
# 4.2 Bash Builtin Commands (enable)
|
| 4343 |
# ==============================================================================
|
| 4344 |
|
| 4345 |
|
| 4346 |
# "Disables the builtins listed in name... Bash searches the PATH"
|
| 4347 |
# We disable 'echo'. To avoid breaking the test runner which relies on echo,
|
| 4348 |
# we wrap this in a subshell or restore it immediately.
|
| 4349 |
# We expect 'echo' to now be found in PATH (external) or fail if PATH is strict.
|
| 4350 |
# We use 'type' to verify it is no longer a builtin.
|
| 4351 |
(
|
| 4352 |
enable -n echo
|
| 4353 |
type -t echo | grep -v "builtin" >/dev/null && echo "disabled"
|
| 4354 |
)
|
| 4355 |
## STDOUT:
|
| 4356 |
disabled
|
| 4357 |
## END
|
| 4358 |
|
| 4359 |
|
| 4360 |
# "Print a list of each builtin... indicating whether it is enabled."
|
| 4361 |
enable -a | grep -q "enable" && echo "found enable"
|
| 4362 |
## STDOUT:
|
| 4363 |
found enable
|
| 4364 |
## END
|
| 4365 |
|
| 4366 |
# ==============================================================================
|
| 4367 |
# 4.2 Bash Builtin Commands (typeset)
|
| 4368 |
# ==============================================================================
|
| 4369 |
|
| 4370 |
|
| 4371 |
# "typeset is obsolete. It is a synonym for declare."
|
| 4372 |
typeset -i x=10
|
| 4373 |
typeset -r y=20
|
| 4374 |
echo "$x $y"
|
| 4375 |
## STDOUT:
|
| 4376 |
10 20
|
| 4377 |
## END
|
| 4378 |
|
| 4379 |
# ==============================================================================
|
| 4380 |
# 4.1 Bourne Shell Builtins (eval quoting)
|
| 4381 |
# ==============================================================================
|
| 4382 |
|
| 4383 |
|
| 4384 |
# A stress test for parsing.
|
| 4385 |
# We want to echo the string: value
|
| 4386 |
foo="value"
|
| 4387 |
cmd="echo \"\$foo\""
|
| 4388 |
eval "$cmd"
|
| 4389 |
## STDOUT:
|
| 4390 |
value
|
| 4391 |
## END
|
| 4392 |
|
| 4393 |
|
| 4394 |
cmd="echo 'hello world'"
|
| 4395 |
eval "$cmd"
|
| 4396 |
## STDOUT:
|
| 4397 |
hello world
|
| 4398 |
## END
|
| 4399 |
|
| 4400 |
# ==============================================================================
|
| 4401 |
# 4.2 Bash Builtin Commands (kill)
|
| 4402 |
# ==============================================================================
|
| 4403 |
|
| 4404 |
|
| 4405 |
# "List the signal names."
|
| 4406 |
# We check for a standard signal name like INT or SIGINT.
|
| 4407 |
kill -l | grep -E "INT|SIGINT" >/dev/null && echo "found INT"
|
| 4408 |
## STDOUT:
|
| 4409 |
found INT
|
| 4410 |
## END
|
| 4411 |
|
| 4412 |
|
| 4413 |
# "If any argument is a number, print the signal name... that number represents"
|
| 4414 |
# 128 + 2 (SIGINT) = 130
|
| 4415 |
kill -l 130 | grep -E "INT|SIGINT" >/dev/null && echo "found INT"
|
| 4416 |
## STDOUT:
|
| 4417 |
found INT
|
| 4418 |
## END
|
| 4419 |
|
| 4420 |
# ==============================================================================
|
| 4421 |
# 5.2 Bash Variables (TIMEFORMAT)
|
| 4422 |
# ==============================================================================
|
| 4423 |
|
| 4424 |
|
| 4425 |
# "The value of this parameter is used as a format string specifying how the timing information for pipelines prefixed with the time reserved word is displayed."
|
| 4426 |
# We output real time only (%R).
|
| 4427 |
# Note: 'time' writes to stderr.
|
| 4428 |
(
|
| 4429 |
TIMEFORMAT="%R"
|
| 4430 |
time sleep 0.01
|
| 4431 |
) 2>&1 | grep -E "^0\.0" >/dev/null && echo "formatted"
|
| 4432 |
## STDOUT:
|
| 4433 |
formatted
|
| 4434 |
## END
|
| 4435 |
|
| 4436 |
# ==============================================================================
|
| 4437 |
# 6.4 Bash Conditional Expressions (Legacy)
|
| 4438 |
# ==============================================================================
|
| 4439 |
|
| 4440 |
|
| 4441 |
# "True if the shell option option is enabled."
|
| 4442 |
# (Deprecated in favor of [[ -o ]], but allowed in [ -o ])
|
| 4443 |
set -o errexit
|
| 4444 |
if [ -o errexit ]; then echo "errexit on"; fi
|
| 4445 |
set +o errexit
|
| 4446 |
if [ ! -o errexit ]; then echo "errexit off"; fi
|
| 4447 |
## STDOUT:
|
| 4448 |
errexit on
|
| 4449 |
errexit off
|
| 4450 |
## END
|
| 4451 |
|
| 4452 |
# ==============================================================================
|
| 4453 |
# 3.5.3 Shell Parameter Expansion (Offset/Length Unset)
|
| 4454 |
# ==============================================================================
|
| 4455 |
|
| 4456 |
|
| 4457 |
# "If parameter is unset... expands to the null string unless [default values used]"
|
| 4458 |
unset v
|
| 4459 |
echo "start|${v:0:1}|end"
|
| 4460 |
## STDOUT:
|
| 4461 |
start||end
|
| 4462 |
## END
|
| 4463 |
|
| 4464 |
|
| 4465 |
v=""
|
| 4466 |
echo "start|${v:0:1}|end"
|
| 4467 |
## STDOUT:
|
| 4468 |
start||end
|
| 4469 |
## END
|
| 4470 |
|
| 4471 |
# ==============================================================================
|
| 4472 |
# 3.2.3 Pipelines (Side Effects / Subshells)
|
| 4473 |
# ==============================================================================
|
| 4474 |
|
| 4475 |
|
| 4476 |
# "Each command in a pipeline is executed as a separate process (i.e., in a subshell)."
|
| 4477 |
# (Unless lastpipe is set, which is off by default in non-interactive bash).
|
| 4478 |
x=0
|
| 4479 |
echo "data" | x=1
|
| 4480 |
# In standard Bash, x should still be 0 because x=1 happened in a subshell.
|
| 4481 |
echo "$x"
|
| 4482 |
## STDOUT:
|
| 4483 |
0
|
| 4484 |
## END
|
| 4485 |
|
| 4486 |
|
| 4487 |
# A common mistake: piping into a while loop puts the loop in a subshell.
|
| 4488 |
count=0
|
| 4489 |
echo -e "1\n2" | while read line; do
|
| 4490 |
(( count++ ))
|
| 4491 |
done
|
| 4492 |
# count is lost in Bash
|
| 4493 |
echo "$count"
|
| 4494 |
## STDOUT:
|
| 4495 |
0
|
| 4496 |
## END
|
| 4497 |
|
| 4498 |
# ==============================================================================
|
| 4499 |
# 3.2.5.1 Looping Constructs (Scope)
|
| 4500 |
# ==============================================================================
|
| 4501 |
|
| 4502 |
|
| 4503 |
# Loop variables are global, not local to the loop.
|
| 4504 |
x="original"
|
| 4505 |
for x in "changed"; do
|
| 4506 |
:
|
| 4507 |
done
|
| 4508 |
echo "$x"
|
| 4509 |
## STDOUT:
|
| 4510 |
changed
|
| 4511 |
## END
|
| 4512 |
|
| 4513 |
|
| 4514 |
# "If 'in words' is not present... [uses positional params]. If 'in words' IS present but empty..."
|
| 4515 |
# Valid syntax, does nothing.
|
| 4516 |
for i in ; do
|
| 4517 |
echo "should not run"
|
| 4518 |
done
|
| 4519 |
echo "done"
|
| 4520 |
## STDOUT:
|
| 4521 |
done
|
| 4522 |
## END
|
| 4523 |
|
| 4524 |
# ==============================================================================
|
| 4525 |
# 3.5 Shell Expansions (Order of Operations)
|
| 4526 |
# ==============================================================================
|
| 4527 |
|
| 4528 |
|
| 4529 |
# "After all expansions, quote removal is performed."
|
| 4530 |
# If a glob matches a file named '$var', that filename should NOT be expanded as a variable.
|
| 4531 |
touch '$var'
|
| 4532 |
var="expanded"
|
| 4533 |
# Echoing the glob should print '$var' (literal filename), not 'expanded'.
|
| 4534 |
# We use set -f to ensure we don't accidentally glob something else, but here we WANT glob.
|
| 4535 |
echo *var*
|
| 4536 |
rm '$var'
|
| 4537 |
## STDOUT:
|
| 4538 |
$var
|
| 4539 |
## END
|
| 4540 |
|
| 4541 |
|
| 4542 |
# Circular reference check.
|
| 4543 |
# x holds "x". ${!x} -> value of x -> "x".
|
| 4544 |
x=x
|
| 4545 |
echo "${!x}"
|
| 4546 |
## STDOUT:
|
| 4547 |
x
|
| 4548 |
## END
|
| 4549 |
|
| 4550 |
# ==============================================================================
|
| 4551 |
# 4.2 Bash Builtin Commands (unalias)
|
| 4552 |
# ==============================================================================
|
| 4553 |
|
| 4554 |
|
| 4555 |
# "Remove all alias definitions."
|
| 4556 |
shopt -s expand_aliases
|
| 4557 |
alias foo=echo
|
| 4558 |
unalias -a
|
| 4559 |
# 'foo' should now be "not found" or generic command (if foo existed), here it shouldn't exist.
|
| 4560 |
type foo >/dev/null 2>&1 || echo "removed"
|
| 4561 |
## STDOUT:
|
| 4562 |
removed
|
| 4563 |
## END
|
| 4564 |
|
| 4565 |
# ==============================================================================
|
| 4566 |
# 6.4 Bash Conditional Expressions (Ambiguity)
|
| 4567 |
# ==============================================================================
|
| 4568 |
|
| 4569 |
|
| 4570 |
# Inside [[ ]], -f is a unary operator (file exists).
|
| 4571 |
# Inside [ ], it might be parsed as the string "-f" if arguments are missing.
|
| 4572 |
# [[ -f ]] with no argument is syntax error?
|
| 4573 |
# [[ "-f" ]] is a string test (true).
|
| 4574 |
if [[ "-f" ]]; then echo "string true"; fi
|
| 4575 |
## STDOUT:
|
| 4576 |
string true
|
| 4577 |
## END
|
| 4578 |
|
| 4579 |
|
| 4580 |
# [ -f ] tests if the string "-f" is non-empty? Or is it a missing argument error?
|
| 4581 |
# POSIX: "1 argument: Exit true if not null."
|
| 4582 |
if [ -f ]; then echo "string true"; fi
|
| 4583 |
## STDOUT:
|
| 4584 |
string true
|
| 4585 |
## END
|
| 4586 |
|
| 4587 |
# ==============================================================================
|
| 4588 |
# 3.2.5.3 Grouping Commands (Redirection)
|
| 4589 |
# ==============================================================================
|
| 4590 |
|
| 4591 |
|
| 4592 |
# Apply redirection to the entire brace group.
|
| 4593 |
{
|
| 4594 |
echo "line1"
|
| 4595 |
echo "line2" >&2
|
| 4596 |
} > group.out 2> group.err
|
| 4597 |
cat group.out
|
| 4598 |
cat group.err
|
| 4599 |
rm group.out group.err
|
| 4600 |
## STDOUT:
|
| 4601 |
line1
|
| 4602 |
line2
|
| 4603 |
## END
|