1 #!/bin/bash
2
3 # NOTE on bash bug: After setting IFS to array, it never splits anymore? Even
4 # if you assign IFS again.
5
6 ### IFS is scoped
7 IFS=b
8 word=abcd
9 f() { local IFS=c; argv.py $word; }
10 f
11 argv.py $word
12 # stdout-json: "['ab', 'd']\n['a', 'cd']\n"
13
14 ### Tilde sub is not split, but var sub is
15 HOME="foo bar"
16 argv.py ~
17 argv.py $HOME
18 # stdout-json: "['foo bar']\n['foo', 'bar']\n"
19
20 ### Word splitting
21 a="1 2"
22 b="3 4"
23 argv.py $a"$b"
24 # stdout-json: "['1', '23 4']\n"
25
26 ### Word splitting 2
27 a="1 2"
28 b="3 4"
29 c="5 6"
30 d="7 8"
31 argv.py $a"$b"$c"$d"
32 # stdout-json: "['1', '23 45', '67 8']\n"
33
34 # Has tests on differences between $* "$*" $@ "$@"
35 # http://stackoverflow.com/questions/448407/bash-script-to-receive-and-repass-quoted-parameters
36
37 ### $*
38 func() { argv.py -$*-; }
39 func "a 1" "b 2" "c 3"
40 # stdout: ['-a', '1', 'b', '2', 'c', '3-']
41
42 ### "$*"
43 func() { argv.py "-$*-"; }
44 func "a 1" "b 2" "c 3"
45 # stdout: ['-a 1 b 2 c 3-']
46
47 ### $@
48 # How does this differ from $* ? I don't think it does.
49 func() { argv.py -$@-; }
50 func "a 1" "b 2" "c 3"
51 # stdout: ['-a', '1', 'b', '2', 'c', '3-']
52
53 ### "$@"
54 func() { argv.py "-$@-"; }
55 func "a 1" "b 2" "c 3"
56 # stdout: ['-a 1', 'b 2', 'c 3-']
57
58 ### empty argv
59 argv.py 1 "$@" 2 $@ 3 "$*" 4 $* 5
60 # stdout: ['1', '2', '3', '', '4', '5']
61
62 ### Word elision with space
63 s1=' '
64 argv.py $s1
65 # stdout: []
66
67 ### Word elision with non-whitespace IFS
68 # Treated differently than the default IFS. What is the rule here?
69 IFS=_
70 s1='_'
71 argv.py $s1
72 # stdout: ['']
73
74 ### Leading/trailing word elision with non-whitespace IFS
75 # This behavior is weird.
76 IFS=_
77 s1='_a_b_'
78 argv.py $s1
79 # stdout: ['', 'a', 'b']
80
81 ### Multiple non-whitespace IFS chars.
82 IFS=_-
83 s1='a__b---c_d'
84 argv.py $s1
85 # stdout: ['a', '', 'b', '', '', 'c', 'd']
86
87 ### IFS with whitespace and non-whitepace.
88 # NOTE: Three delimiters means two empty words in the middle. No elision.
89 IFS='_ '
90 s1='a_b _ _ _ c _d e'
91 argv.py $s1
92 # stdout: ['a', 'b', '', '', 'c', 'd', 'e']
93
94 ### empty $@ and $* is elided
95 func() { argv.py 1 $@ $* 2; }
96 func
97 # stdout: ['1', '2']
98
99 ### unquoted empty arg is elided
100 empty=""
101 argv.py 1 $empty 2
102 # stdout: ['1', '2']
103
104 ### unquoted whitespace arg is elided
105 space=" "
106 argv.py 1 $space 2
107 # stdout: ['1', '2']
108
109 ### empty literals are not elided
110 space=" "
111 argv.py 1 $space"" 2
112 # stdout: ['1', '', '2']
113
114 ### no splitting when IFS is empty
115 IFS=""
116 foo="a b"
117 argv.py $foo
118 # stdout: ['a b']
119
120 ### default value can yield multiple words
121 argv.py 1 ${undefined:-"2 3" "4 5"} 6
122 # stdout: ['1', '2 3', '4 5', '6']
123
124 ### default value can yield multiple words with part joining
125 argv.py 1${undefined:-"2 3" "4 5"}6
126 # stdout: ['12 3', '4 56']
127
128 ### default value with unquoted IFS char
129 IFS=_
130 argv.py 1${undefined:-"2_3"x_x"4_5"}6
131 # stdout: ['12_3x', 'x4_56']
132
133
134 # TODO:
135 # - unquoted args of whitespace are not elided (when IFS = null)
136 # - empty quoted args are kept
137 # - Test ${@:1} and so forth?
138 #
139 # - $* $@ with empty IFS
140 # - $* $@ with custom IFS
141 #
142 # - no splitting when IFS is empty
143 # - word splitting removes leading and trailing whitespace
144
145 # TODO: test framework needs common setup
146
147 # Test IFS and $@ $* on all these
148 ### TODO
149 empty=""
150 space=" "
151 AB="A B"
152 X="X"
153 Yspaces=" Y "