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 ### default value can yield multiple words with part joining
89 argv.py 1${undefined:-"2 3" "4 5"}6
90 # stdout: ['12 3', '4 56']
91
92 ### default value with unquoted IFS char
93 IFS=_
94 argv.py 1${undefined:-"2_3"x_x"4_5"}6
95 # stdout: ['12_3x', 'x4_56']
96
97
98 # TODO:
99 # - unquoted args of whitespace are not elided (when IFS = null)
100 # - empty quoted args are kept
101 # - Test ${@:1} and so forth?
102 #
103 # - $* $@ with empty IFS
104 # - $* $@ with custom IFS
105 #
106 # - no splitting when IFS is empty
107 # - word splitting removes leading and trailing whitespace
108
109 # TODO: test framework needs common setup
110
111 # Test IFS and $@ $* on all these
112 ### TODO
113 empty=""
114 space=" "
115 AB="A B"
116 X="X"
117 Yspaces=" Y "