shell array
A variable that holds a numbered list of separate values, written in bash as names=(one "two words" three).
In more detail
Elements are numbered from 0 and a negative index counts from the end. "${names[@]}" expands to one word per element, keeping spaces inside elements intact, which makes arrays the safe way to build lists of file names or command arguments. Arrays are a bash feature, not part of POSIX sh.
Examples
files=('two words.txt' poem.txt)for f in "${files[@]}"; do echo "$f"; doneRelated terms
- associative array — A bash 4 array indexed by strings instead of numbers, declared with declare -A.
- dollar at — The special parameter $@, which in double quotes expands to every argument, or array element with ${arr[@]}, as a separate word.
- string length expansion — The ${#name} form, which expands to the number of characters in a variable, or ${#array[@]} for the number of elements.
- bashism — A bash-only feature, such as [[ ]], arrays, (( )) or process substitution, used in a script that claims to be POSIX sh.
Taught in
- Which Shell Am I In, And Getting Bash Five — Bash Scripting & Command Line Automation
- The Field Separator, And Reading Lines Safely — Bash Scripting & Command Line Automation
- Arrays, And Why Dollar At Is Not Dollar Star — Bash Scripting & Command Line Automation
- Shell Variables, The Environment And export — Bash Scripting & Command Line Automation
- Functions, local And declare — Bash Scripting & Command Line Automation
- Style That Prevents Bugs — Bash Scripting & Command Line Automation
- POSIX sh Versus Bash — Bash Scripting & Command Line Automation
- eval, And What To Use Instead — Bash Scripting & Command Line Automation