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 fun ( ) { echo in-func; }; fun
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 $foo-bar() { ls ; }
68 ## status: 2
69 ## OK bash/mksh status: 1
70
71 #### Function name with command sub
72 foo-$(echo hi)() { ls ; }
73 ## status: 2
74 ## OK bash/mksh status: 1
75
76 #### Function name with !
77 # bash allows this; dash doesn't.
78 foo!bar() { ls ; }
79 ## status: 0
80 ## OK dash status: 2
81
82 #### Function name with -
83 # bash allows this; dash doesn't.
84 foo-bar() { ls ; }
85 ## status: 0
86 ## OK dash status: 2
87
88 #### Break after ) is OK.
89 # newline is always a token in "normal" state.
90 echo hi; fun ( )
91 { echo in-func; }
92 fun
93 ## STDOUT:
94 hi
95 in-func
96 ## END
97
98 #### Nested definition
99 # A function definition is a command, so it can be nested
100 fun() {
101 nested_func() { echo nested; }
102 nested_func
103 }
104 fun
105 ## stdout: nested
106