| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # TODO: Make this a spec test.
 | 
| 4 | #
 | 
| 5 | # Usage:
 | 
| 6 | #   ./static-vs-dynamic-assignment.sh <function name>
 | 
| 7 | 
 | 
| 8 | argv() {
 | 
| 9 |   spec/bin/argv.py "$@"
 | 
| 10 | }
 | 
| 11 | 
 | 
| 12 | demo() {
 | 
| 13 |   # dash behaves differently here!
 | 
| 14 |   local x='X X'
 | 
| 15 |   local y=$x  # no word splitting!
 | 
| 16 |   argv "$y"
 | 
| 17 | 
 | 
| 18 |   # dash behaves differently here!
 | 
| 19 |   local x='X z=ZZ'
 | 
| 20 |   local y=$x  # no word splitting!
 | 
| 21 |   argv "$y"
 | 
| 22 | 
 | 
| 23 |   # So basically each word is at most one assignment.  That is easy to
 | 
| 24 |   # implement dynamically.  And then we can unify export and local again.
 | 
| 25 | 
 | 
| 26 |   local s='a=AA b=BB c'
 | 
| 27 |   local "$s"     # This is a SINGLE assignment to a
 | 
| 28 |   argv "$a" "$b"
 | 
| 29 |   local $s       # This is multiple assignments to a, b, and c
 | 
| 30 |                  # zsh behaves differently!
 | 
| 31 |   argv "$a" "$b"
 | 
| 32 | 
 | 
| 33 |   local d='a=$a'  # note the single quotes!  It is NOT evaluated.
 | 
| 34 |   local $d
 | 
| 35 |   argv "$d"
 | 
| 36 |   local "$d"
 | 
| 37 |   argv "$d"
 | 
| 38 | 
 | 
| 39 |   local -a array
 | 
| 40 |   array=(1 2 3)  # mksh doesn't allow initialization on one line
 | 
| 41 | 
 | 
| 42 |   # Doh, this is parsed by bash and zsh too!  And mksh somewhat supports it.
 | 
| 43 |   #
 | 
| 44 |   # So how do we re-run the parser?  Or should we omit this for now?
 | 
| 45 |   # Don't allow dynamic a[x] assignments?  But 3 shells all implement it.
 | 
| 46 | 
 | 
| 47 |   local e='array[1+1]=42'
 | 
| 48 |   local "$e"     # This is a SINGLE assignment to a
 | 
| 49 |   argv "${array[@]}"
 | 
| 50 | 
 | 
| 51 |   # Hm why is this a parse error in bash!!
 | 
| 52 |   # zsh allows this!!!
 | 
| 53 |   # So it dequotes them first?  This makes no sense honestly.
 | 
| 54 |   #local s='x="1 2 3"'
 | 
| 55 |   local s="x='1 2 3'"
 | 
| 56 |   local $s
 | 
| 57 |   argv "$x"
 | 
| 58 | 
 | 
| 59 |   local s='x=1 2 3'
 | 
| 60 |   # This results in a parse error!
 | 
| 61 |   local $s
 | 
| 62 |   argv "$x"
 | 
| 63 | }
 | 
| 64 | 
 | 
| 65 | # dash is wrong
 | 
| 66 | dash_demo() {
 | 
| 67 |   dash $0 demo
 | 
| 68 | }
 | 
| 69 | 
 | 
| 70 | # zsh is most lenient
 | 
| 71 | zsh_demo() {
 | 
| 72 |   zsh $0 demo
 | 
| 73 | }
 | 
| 74 | 
 | 
| 75 | # mksh behaves more like bash
 | 
| 76 | mksh_demo() {
 | 
| 77 |   mksh $0 demo
 | 
| 78 | }
 | 
| 79 | 
 | 
| 80 | "$@"
 |