1 |
#!/usr/bin/env bash |
2 |
# |
3 |
# NOTE: |
4 |
# - $! is tested in background.test.sh |
5 |
# - $- is tested in sh-options |
6 |
|
7 |
### $PWD |
8 |
# Just test that it has a slash for now. |
9 |
echo $PWD | grep -o / |
10 |
# status: 0 |
11 |
|
12 |
### $1 .. $9 are scoped, while $0 is not |
13 |
func() { echo $0 $1 $2 | sed -e 's/.*sh/sh/'; } |
14 |
func a b |
15 |
# stdout: sh a b |
16 |
|
17 |
### $? |
18 |
echo $? # starts out as 0 |
19 |
sh -c 'exit 33' |
20 |
echo $? |
21 |
# stdout-json: "0\n33\n" |
22 |
# status: 0 |
23 |
|
24 |
### $# |
25 |
set -- 1 2 3 4 |
26 |
echo $# |
27 |
# stdout: 4 |
28 |
# status: 0 |
29 |
|
30 |
### $_ |
31 |
# This is bash-specific. |
32 |
echo hi |
33 |
echo $_ |
34 |
# stdout-json: "hi\nhi\n" |
35 |
# N-I dash/mksh stdout-json: "hi\n\n" |
36 |
|
37 |
### $$ looks like a PID |
38 |
# Just test that it has decimal digits |
39 |
echo $$ | egrep '[0-9]+' |
40 |
# status: 0 |
41 |
|
42 |
### $$ doesn't change with subshell |
43 |
# Just test that it has decimal digits |
44 |
set -o errexit |
45 |
die() { |
46 |
echo 1>&2 "$@"; exit 1 |
47 |
} |
48 |
parent=$$ |
49 |
test -n "$parent" || die "empty PID in parent" |
50 |
( child=$$ |
51 |
test -n "$child" || die "empty PID in child" |
52 |
test "$parent" = "$child" || die "should be equal: $parent != $child" |
53 |
) |
54 |
exit 3 # make sure we got here |
55 |
# stdout-json: "" |
56 |
# status: 3 |
57 |
|
58 |
### $BASHPID DOES change with subshell |
59 |
set -o errexit |
60 |
die() { |
61 |
echo 1>&2 "$@"; exit 1 |
62 |
} |
63 |
parent=$BASHPID |
64 |
test -n "$parent" || die "empty BASHPID in parent" |
65 |
( child=$BASHPID |
66 |
test -n "$child" || die "empty BASHPID in child" |
67 |
test "$parent" != "$child" || die "should not be equal: $parent = $child" |
68 |
) |
69 |
exit 3 # make sure we got here |
70 |
# stdout-json: "" |
71 |
# status: 3 |
72 |
# N-I dash status: 1 |
73 |
|
74 |
### Background PID $! looks like a PID |
75 |
sleep 0.01 & |
76 |
pid=$! |
77 |
wait |
78 |
echo $pid | egrep '[0-9]+' >/dev/null |
79 |
echo status=$? |
80 |
# stdout: status=0 |
81 |
|
82 |
### $PPID |
83 |
echo $PPID | egrep '[0-9]+' |
84 |
# status: 0 |
85 |
|
86 |
# NOTE: There is also $BASHPID |
87 |
|
88 |
### $PIPESTATUS |
89 |
echo hi | sh -c 'cat; exit 33' | wc -l >/dev/null |
90 |
argv.py "${PIPESTATUS[@]}" |
91 |
# status: 0 |
92 |
# stdout: ['0', '33', '0'] |
93 |
# N-I dash stdout-json: "" |
94 |
# N-I dash status: 2 |
95 |
|
96 |
### $RANDOM |
97 |
expr $0 : '.*/osh$' && exit 99 # Disabled because of spec-runner.sh issue |
98 |
echo $RANDOM | egrep '[0-9]+' |
99 |
# status: 0 |
100 |
# N-I dash status: 1 |