| 1 | #!/bin/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 | |
| 25 | ### Function with spaces, to see if ( and ) are separate tokens. |
| 26 | # NOTE: Newline after ( is not OK. |
| 27 | func ( ) { echo in-func; }; func |
| 28 | # stdout: in-func |
| 29 | |
| 30 | ### subshell function |
| 31 | # bash allows this. |
| 32 | i=0 |
| 33 | j=0 |
| 34 | inc() { i=$((i+5)); } |
| 35 | inc_subshell() ( j=$((j+5)); ) |
| 36 | inc |
| 37 | inc_subshell |
| 38 | echo $i $j |
| 39 | # stdout: 5 0 |
| 40 | |
| 41 | ### Hard case, function with } token in it |
| 42 | rbrace() { echo }; }; rbrace |
| 43 | # stdout: } |
| 44 | |
| 45 | ### . in function name |
| 46 | # bash accepts; dash doesn't |
| 47 | func-name.ext ( ) { echo func-name.ext; } |
| 48 | func-name.ext |
| 49 | # stdout: func-name.ext |
| 50 | # OK dash status: 2 |
| 51 | # OK dash stdout-json: "" |
| 52 | |
| 53 | ### = in function name |
| 54 | # WOW, bash is so lenient. foo=bar is a command, I suppose. I think I'm doing |
| 55 | # to disallow this one. |
| 56 | func-name=ext ( ) { echo func-name=ext; } |
| 57 | func-name=ext |
| 58 | # stdout: func-name=ext |
| 59 | # OK dash status: 2 |
| 60 | # OK dash stdout-json: "" |
| 61 | # OK mksh status: 1 |
| 62 | # OK mksh stdout-json: "" |
| 63 | |
| 64 | ### Function name with $ |
| 65 | # bash allows this; dash doesn't. |
| 66 | foo$bar() { ls ; } |
| 67 | # status: 2 |
| 68 | # OK bash/mksh status: 1 |
| 69 | |
| 70 | ### Function name with ! |
| 71 | # bash allows this; dash doesn't. |
| 72 | foo!bar() { ls ; } |
| 73 | # status: 0 |
| 74 | # OK dash status: 2 |
| 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 | ### Break after ) is OK. |
| 83 | # newline is always a token in "normal" state. |
| 84 | echo hi; func ( ) |
| 85 | { echo in-func; } |
| 86 | func |
| 87 | # stdout-json: "hi\nin-func\n" |
| 88 | |
| 89 | ### Nested definition |
| 90 | # A function definition is a command, so it can be nested |
| 91 | func() { |
| 92 | nested_func() { echo nested; } |
| 93 | nested_func |
| 94 | } |
| 95 | func |
| 96 | # stdout: nested |
| 97 |