1 |
#!/usr/bin/env bash |
2 |
|
3 |
#### Lazy Evaluation of Alternative |
4 |
i=0 |
5 |
x=x |
6 |
echo ${x:-$((i++))} |
7 |
echo $i |
8 |
echo ${undefined:-$((i++))} |
9 |
echo $i # i is one because the alternative was only evaluated once |
10 |
## status: 0 |
11 |
## stdout-json: "x\n0\n0\n1\n" |
12 |
## N-I dash status: 2 |
13 |
## N-I dash stdout-json: "x\n0\n" |
14 |
|
15 |
#### Default value when empty |
16 |
empty='' |
17 |
echo ${empty:-is empty} |
18 |
## stdout: is empty |
19 |
|
20 |
#### Default value when unset |
21 |
echo ${unset-is unset} |
22 |
## stdout: is unset |
23 |
|
24 |
#### Unquoted with array as default value |
25 |
set -- '1 2' '3 4' |
26 |
argv.py X${unset=x"$@"x}X |
27 |
## stdout: ['Xx1', '2', '3', '4xX'] |
28 |
|
29 |
#### Quoted with array as default value |
30 |
set -- '1 2' '3 4' |
31 |
argv.py "X${unset=x"$@"x}X" |
32 |
## stdout: ['Xx1 2 3 4xX'] |
33 |
## BUG bash stdout: ['Xx1', '2', '3', '4xX'] |
34 |
|
35 |
#### Assign default with array |
36 |
# HMMMM osh stdout-json: "['Xx1 2', '3 4xX']\n['x1 2 3 4x']\n" |
37 |
# I think OSH diverges here because VTest is different than VOp1? |
38 |
# I parse all the arg words the same way. |
39 |
set -- '1 2' '3 4' |
40 |
argv.py X${unset=x"$@"x}X |
41 |
argv.py "$unset" |
42 |
## STDOUT: |
43 |
['Xx1', '2', '3', '4xX'] |
44 |
['x1 2 3 4x'] |
45 |
## #END |
46 |
|
47 |
#### Assign default value when empty |
48 |
empty='' |
49 |
${empty:=is empty} |
50 |
echo $empty |
51 |
## stdout: is empty |
52 |
|
53 |
#### Assign default value when unset |
54 |
${unset=is unset} |
55 |
echo $unset |
56 |
## stdout: is unset |
57 |
|
58 |
#### Alternative value when empty |
59 |
v=foo |
60 |
empty='' |
61 |
echo ${v:+v is not empty} ${empty:+is not empty} |
62 |
## stdout: v is not empty |
63 |
|
64 |
#### Alternative value when unset |
65 |
v=foo |
66 |
echo ${v+v is not unset} ${unset:+is not unset} |
67 |
## stdout: v is not unset |
68 |
|
69 |
#### Error when empty |
70 |
empty='' |
71 |
echo ${empty:?'is em'pty} # test eval of error |
72 |
echo should not get here |
73 |
## stdout-json: "" |
74 |
## status: 1 |
75 |
## OK dash status: 2 |
76 |
|
77 |
#### Error when unset |
78 |
echo ${unset?is empty} |
79 |
echo should not get here |
80 |
## stdout-json: "" |
81 |
## status: 1 |
82 |
## OK dash status: 2 |
83 |
|
84 |
#### Error when unset |
85 |
v=foo |
86 |
echo ${v+v is not unset} ${unset:+is not unset} |
87 |
## stdout: v is not unset |