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 $@ and $* is elided
59 func() { argv.py 1 $@ $* 2; }
60 func
61 # stdout: ['1', '2']
62
63 ### unquoted empty arg is elided
64 empty=""
65 argv.py 1 $empty 2
66 # stdout: ['1', '2']
67
68 ### unquoted whitespace arg is elided
69 space=" "
70 argv.py 1 $space 2
71 # stdout: ['1', '2']
72
73 ### empty literals are not elided
74 space=" "
75 argv.py 1 $space"" 2
76 # stdout: ['1', '', '2']
77
78 ### no splitting when IFS is empty
79 IFS=""
80 foo="a b"
81 argv.py $foo
82 # stdout: ['a b']
83
84 ### default value can yield multiple words
85 argv.py 1 ${undefined:-"2 3" "4 5"} 6
86 # stdout: ['1', '2 3', '4 5', '6']
87
88
89 # TODO:
90 # - unquoted args of whitespace are not elided (when IFS = null)
91 # - empty quoted args are kept
92 # - Test ${@:1} and so forth?
93 #
94 # - $* $@ with empty IFS
95 # - $* $@ with custom IFS
96 #
97 # - no splitting when IFS is empty
98 # - word splitting removes leading and trailing whitespace
99
100 # TODO: test framework needs common setup
101
102 # Test IFS and $@ $* on all these
103 ### TODO
104 empty=""
105 space=" "
106 AB="A B"
107 X="X"
108 Yspaces=" Y "