1 #!/usr/bin/env bash
2
3 ### Incomplete Function
4 # code: foo()
5 # status: 2
6 # BUG mksh status: 0
7
8 ### Incomplete Function 2
9 # code: foo() {
10 # status: 2
11 # OK mksh status: 1
12
13 ### Bad function
14 # code: foo(ls)
15 # status: 2
16 # OK mksh status: 1
17
18 ### Unbraced function body.
19 # dash allows this, but bash does not. The POSIX grammar might not allow
20 # this? Because a function body needs a compound command.
21 # function_body : compound_command
22 # | compound_command redirect_list /* Apply rule 9 */
23 # code: one_line() ls; one_line;
24 # status: 0
25 # OK bash/osh status: 2
26
27 ### Function with spaces, to see if ( and ) are separate tokens.
28 # NOTE: Newline after ( is not OK.
29 func ( ) { echo in-func; }; func
30 # stdout: in-func
31
32 ### subshell function
33 # bash allows this.
34 i=0
35 j=0
36 inc() { i=$((i+5)); }
37 inc_subshell() ( j=$((j+5)); )
38 inc
39 inc_subshell
40 echo $i $j
41 # stdout: 5 0
42
43 ### Hard case, function with } token in it
44 rbrace() { echo }; }; rbrace
45 # stdout: }
46
47 ### . in function name
48 # bash accepts; dash doesn't
49 func-name.ext ( ) { echo func-name.ext; }
50 func-name.ext
51 # stdout: func-name.ext
52 # OK dash status: 2
53 # OK dash stdout-json: ""
54
55 ### = in function name
56 # WOW, bash is so lenient. foo=bar is a command, I suppose. I think I'm doing
57 # to disallow this one.
58 func-name=ext ( ) { echo func-name=ext; }
59 func-name=ext
60 # stdout: func-name=ext
61 # OK dash status: 2
62 # OK dash stdout-json: ""
63 # OK mksh status: 1
64 # OK mksh stdout-json: ""
65
66 ### Function name with $
67 # bash allows this; dash doesn't.
68 foo$bar() { ls ; }
69 # status: 2
70 # OK bash/mksh status: 1
71
72 ### Function name with !
73 # bash allows this; dash doesn't.
74 foo!bar() { ls ; }
75 # status: 0
76 # OK dash status: 2
77
78 ### Function name with -
79 # bash allows this; dash doesn't.
80 foo-bar() { ls ; }
81 # status: 0
82 # OK dash status: 2
83
84 ### Break after ) is OK.
85 # newline is always a token in "normal" state.
86 echo hi; func ( )
87 { echo in-func; }
88 func
89 # stdout-json: "hi\nin-func\n"
90
91 ### Nested definition
92 # A function definition is a command, so it can be nested
93 func() {
94 nested_func() { echo nested; }
95 nested_func
96 }
97 func
98 # stdout: nested
99