Skip to content

Latest commit

 

History

History
53 lines (36 loc) · 611 Bytes

bash-array.md

File metadata and controls

53 lines (36 loc) · 611 Bytes

arrays

declare -a foo  # indexed
declare -A bar  # associative
foo=(foo bar baz bat banan)
bar=([foo]=1 [bar]=2 [baz]=3)

Note: arrays cannot be export-ed.

use as argument

# As a single string
echo "${foo[*]}"

# One string per array item
echo "${foo[@]}"

mutate

# append two items
foo+=(bar baz)

access

echo "${foo[1]}"
echo "${bar[baz]}"

slice

# first two elements
${foo[@]::2}

# minus the first two elements
${foo[@]:2}

# last two elements
${foo[@]:${#foo[@]}-2}

# minus the last two elements
${foo[@]::${#foo[@]}-2}