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