source | all docs for version 0.7.pre8 | all versions | oilshell.org
Warning: Work in progress! Leave feedback on Zulip or Github if you'd like this doc to be updated.
This doc internal data structure in the Oil interpreter, and gives examples of how you manipulate them with shell or Oil code.
The interpreter is "unified".
The goal of Oil is to replace this quirky language. But we still made it compatible.
If you want to write scripts compatible with OSH and bash.
It's meant to be more sane.
See Known Differences.
I salvaged these semantics.
Worst of the language! Newest and most "grafted on".
Parsing bash is undecidable.
A[x]
a[x]
Horrible
a=('1 2' 3)
b=(1 '2 3') # two different elements
[[ $a == $b ]]
[[ ${a[0]} == ${b[0]} ]]
[[ ${a[@]} == ${b[@]} ]]
Associative arrays and being undefined
set -o nounset
(in bash 4.3). I can't recommend
in good faithShell has a stack but no heap. It has values and locations, but no references/pointers.
Oil adds references to data structures on the heap, which may be recurisve.
We save those for Oil!
There are lots of coercions instead.
bash has '-i' but that's true anyway.
You can't unset an array in OSH? But you can in bash.
declare -a array
declare -a array=()
declare -A assoc
# there is no empty literal here
Also valid, but not necessary since declare
is local:
local -a array
local -A assoc
Makes a global array:
array=()
Respects the normal rules of argv.
prefix=foo
myarray=(one two -{three,four}- {5..8} *.py "$prefix*.py" '$prefix*.py')
myarray=(
$var ${var} "$var"
$(echo hi) "$(echo hi)"
$(1 + 2 * 3)
)
(['k']=v)
Unlike bash, ([0]=v) is still an associative array literal.
It's not an indexed array literal. This matters when you take slices and
so forth?
echo "${array[@]}"
echo "${assoc[@]}"
Not Allowed, unlike in bash!
$assoc ${assoc} "${assoc}"
${!assoc} ${assoc//pattern/replace} # etc.
Note that since a for loop takes an array of words, evaluating/splicing works:
for i in "${a1[@]}" "${a2[@]}"; do
echo $i
done
echo ${#array[@]}
echo ${#assoc[@]}
echo ${!array[@]}
echo ${!assoc[@]}
echo ${!array[*]}
echo ${!assoc[*]}
echo "${!array[*]}"
echo "${!assoc[*]}"
matrix: a['x'] a["x"] a["$x"] a[$x] a[${x}] a[${x#a}]
a[x] -- allowed
A[x] -- NOT allowed? It should be a string
(( 'a' )) -- parsed, but can't evaluate
# This is a string in both cases
a[0]
A[0]
undef[0]=1 automatically creates an INDEXED array undef=(1)
a[expr]= # int_coerce
A[expr]= # no integer coercion
Just like you can append to strings:
s+='foo'
Append to elements of an array, which are strings:
a[x+1]+=x
a[x+1]+=$x
${array[@]:1:3}
Note the presence of DISALLOWED VALUES.
# TODO: disallow this? because no order
${assoc[@]:1:3}
NOTE: string slicing:
a=(1 2 3)
a+=(4 5 6)
echo ${!array[@]}
echo ${!assoc[@]}
echo ${array[@]//x/X}
echo ${assoc[@]//x/X}
Mentioned above:
a[x+1]+=x
a[x+1]+=$x
s+='foo'
Mentioned above:
echo ${a[0]}
echo "${a[0]}"
echo ${a[i+1]}
SURPRISING! Avoid if you can!!!
(( a[ x+1 ] += s )) #
Operates on strings only. Can't compare
Oil supports various shell and bash operations to view the interpretr state.
set
prints variables and their valuesset -o
prints optionsdeclare/typeset/readonly/export -p
prints a subset of variablestest -v
tests if a variable is defined.Pretty prints state.