1 #!/usr/bin/env bash
2 #
3 # NOTE:
4 # - $! is tested in background.test.sh
5 # - $- is tested in sh-options
6 #
7 # TODO: It would be nice to make a table, like:
8 #
9 # $$ $BASHPID $PPID $SHLVL $BASH_SUBSHELL
10 # X
11 # (Subshell, Command Sub, Pipeline, Spawn $0)
12 #
13 # And see whether the variable changed.
14
15 #### $PWD is set
16 # Just test that it has a slash for now.
17 echo $PWD | grep /
18 ## status: 0
19
20 #### $PWD is not only set, but exported
21 env | grep PWD
22 ## status: 0
23 ## BUG mksh status: 1
24
25 #### $HOME is NOT set
26 home=$(echo $HOME)
27 test "$home" = ""
28 echo status=$?
29
30 env | grep HOME
31 echo status=$?
32
33 # not in interactive shell either
34 $SH -i -c 'echo $HOME' | grep /
35 echo status=$?
36
37 ## STDOUT:
38 status=0
39 status=1
40 status=1
41 ## END
42
43
44 #### $1 .. $9 are scoped, while $0 is not
45 func() { echo $0 $1 $2 | sed -e 's/.*sh/sh/'; }
46 func a b
47 ## stdout: sh a b
48
49 #### $?
50 echo $? # starts out as 0
51 sh -c 'exit 33'
52 echo $?
53 ## stdout-json: "0\n33\n"
54 ## status: 0
55
56 #### $#
57 set -- 1 2 3 4
58 echo $#
59 ## stdout: 4
60 ## status: 0
61
62 #### $_
63 # This is bash-specific.
64 echo hi
65 echo $_
66 ## stdout-json: "hi\nhi\n"
67 ## N-I dash/mksh stdout-json: "hi\n\n"
68
69 #### $$ looks like a PID
70 # Just test that it has decimal digits
71 echo $$ | egrep '[0-9]+'
72 ## status: 0
73
74 #### $$ doesn't change with subshell or command sub
75 # Just test that it has decimal digits
76 set -o errexit
77 die() {
78 echo 1>&2 "$@"; exit 1
79 }
80 parent=$$
81 test -n "$parent" || die "empty PID in parent"
82 ( child=$$
83 test -n "$child" || die "empty PID in subshell"
84 test "$parent" = "$child" || die "should be equal: $parent != $child"
85 echo 'subshell OK'
86 )
87 echo $( child=$$
88 test -n "$child" || die "empty PID in command sub"
89 test "$parent" = "$child" || die "should be equal: $parent != $child"
90 echo 'command sub OK'
91 )
92 exit 3 # make sure we got here
93 ## status: 3
94 ## STDOUT:
95 subshell OK
96 command sub OK
97 ## END
98
99 #### $BASHPID DOES change with subshell and command sub
100 set -o errexit
101 die() {
102 echo 1>&2 "$@"; exit 1
103 }
104 parent=$BASHPID
105 test -n "$parent" || die "empty BASHPID in parent"
106 ( child=$BASHPID
107 test -n "$child" || die "empty BASHPID in subshell"
108 test "$parent" != "$child" || die "should not be equal: $parent = $child"
109 echo 'subshell OK'
110 )
111 echo $( child=$BASHPID
112 test -n "$child" || die "empty BASHPID in command sub"
113 test "$parent" != "$child" ||
114 die "should not be equal: $parent = $child"
115 echo 'command sub OK'
116 )
117 exit 3 # make sure we got here
118 ## status: 3
119 ## STDOUT:
120 subshell OK
121 command sub OK
122 ## END
123 ## N-I dash status: 1
124 ## N-I dash stdout-json: ""
125
126 #### Background PID $! looks like a PID
127 sleep 0.01 &
128 pid=$!
129 wait
130 echo $pid | egrep '[0-9]+' >/dev/null
131 echo status=$?
132 ## stdout: status=0
133
134 #### $PPID
135 echo $PPID | egrep '[0-9]+'
136 ## status: 0
137
138 # NOTE: There is also $BASHPID
139
140 #### $PIPESTATUS
141 echo hi | sh -c 'cat; exit 33' | wc -l >/dev/null
142 argv.py "${PIPESTATUS[@]}"
143 ## status: 0
144 ## stdout: ['0', '33', '0']
145 ## N-I dash stdout-json: ""
146 ## N-I dash status: 2
147
148 #### $RANDOM
149 expr $0 : '.*/osh$' && exit 99 # Disabled because of spec-runner.sh issue
150 echo $RANDOM | egrep '[0-9]+'
151 ## status: 0
152 ## N-I dash status: 1
153
154 #### $UID and $EUID
155 # These are both bash-specific.
156 set -o errexit
157 echo $UID | egrep -o '[0-9]+' >/dev/null
158 echo $EUID | egrep -o '[0-9]+' >/dev/null
159 echo status=$?
160 ## stdout: status=0
161 ## N-I dash/mksh stdout-json: ""
162 ## N-I dash/mksh status: 1
163
164 #### $OSTYPE is non-empty
165 test -n "$OSTYPE"
166 echo status=$?
167 ## STDOUT:
168 status=0
169 ## END
170 ## N-I dash/mksh STDOUT:
171 status=1
172 ## END
173
174 #### $HOSTNAME
175 test "$HOSTNAME" = "$(hostname)"
176 echo status=$?
177 ## STDOUT:
178 status=0
179 ## END
180 ## N-I dash/mksh STDOUT:
181 status=1
182 ## END
183