| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Tests for bash's type flags on cells. Hopefully we don't have to implement |
| 4 | # this, but it's good to know the behavior. |
| 5 | # |
| 6 | # OSH follows a Python-ish model of types carried with values/objects, not |
| 7 | # locations. |
| 8 | # |
| 9 | # See https://github.com/oilshell/oil/issues/26 |
| 10 | |
| 11 | ### declare -i |
| 12 | declare s |
| 13 | s='1 ' |
| 14 | s+=' 2 ' # string append |
| 15 | declare -i i |
| 16 | i='1 ' |
| 17 | i+=' 2 ' # arith add |
| 18 | declare -i j |
| 19 | j=x # treated like zero |
| 20 | j+=' 2 ' # arith add |
| 21 | echo "$s|$i|$j" |
| 22 | # stdout: 1 2 |3|2 |
| 23 | |
| 24 | ### append in arith context |
| 25 | declare s |
| 26 | (( s='1 ')) |
| 27 | (( s+=' 2 ')) # arith add |
| 28 | declare -i i |
| 29 | (( i='1 ' )) |
| 30 | (( i+=' 2 ' )) |
| 31 | declare -i j |
| 32 | (( j='x ' )) # treated like zero |
| 33 | (( j+=' 2 ' )) |
| 34 | echo "$s|$i|$j" |
| 35 | # stdout: 3|3|2 |
| 36 | |
| 37 | ### declare array vs. string: mixing -a +a and () '' |
| 38 | # dynamic parsing of first argument. |
| 39 | declare +a 'xyz1=1' |
| 40 | declare +a 'xyz2=(2 3)' |
| 41 | declare -a 'xyz3=4' |
| 42 | declare -a 'xyz4=(5 6)' |
| 43 | argv.py "${xyz1}" "${xyz2}" "${xyz3[@]}" "${xyz4[@]}" |
| 44 | # stdout: ['1', '(2 3)', '4', '5', '6'] |
| 45 | |
| 46 | ### declare array vs. associative array |
| 47 | # Hm I don't understand why the array only has one element. I guess because |
| 48 | # index 0 is used twice? |
| 49 | declare -a 'array=([a]=b [c]=d)' |
| 50 | declare -A 'assoc=([a]=b [c]=d)' |
| 51 | argv.py "${#array[@]}" "${!array[@]}" "${array[@]}" |
| 52 | argv.py "${#assoc[@]}" "${!assoc[@]}" "${assoc[@]}" |
| 53 | # stdout-json: "['1', '0', 'd']\n['2', 'a', 'c', 'b', 'd']\n" |