1 |
#!/bin/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 value when empty |
36 |
empty='' |
37 |
${empty:=is empty} |
38 |
echo $empty |
39 |
# stdout: is empty |
40 |
|
41 |
### Assign default value when unset |
42 |
${unset=is unset} |
43 |
echo $unset |
44 |
# stdout: is unset |
45 |
|
46 |
### Assign default with array |
47 |
# HMMMM osh stdout-json: "['Xx1 2', '3 4xX']\n['x1 2 3 4x']\n" |
48 |
# I think OSH diverges here because VTest is different than VOp1? |
49 |
# I parse all the arg words the same way. |
50 |
set -- '1 2' '3 4' |
51 |
argv.py X${unset=x"$@"x}X |
52 |
argv.py "$unset" |
53 |
# stdout-json: "['Xx1', '2', '3', '4xX']\n['x1 2 3 4x']\n" |
54 |
|
55 |
### Alternative value when empty |
56 |
v=foo |
57 |
empty='' |
58 |
echo ${v:+v is not empty} ${empty:+is not empty} |
59 |
# stdout: v is not empty |
60 |
|
61 |
### Alternative value when unset |
62 |
v=foo |
63 |
echo ${v+v is not unset} ${unset:+is not unset} |
64 |
# stdout: v is not unset |
65 |
|
66 |
### Error when empty |
67 |
empty='' |
68 |
${empty:?is empty} |
69 |
# status: 1 |
70 |
# OK dash status: 2 |
71 |
|
72 |
### Error when unset |
73 |
${unset?is empty} |
74 |
# status: 1 |
75 |
# OK dash status: 2 |
76 |
|
77 |
### Error when unset |
78 |
v=foo |
79 |
echo ${v+v is not unset} ${unset:+is not unset} |
80 |
# stdout: v is not unset |