Differences between revisions 1 and 7 (spanning 6 versions)
Revision 1 as of 2007-05-02 22:48:54
Size: 2248
Editor: redondos
Comment:
Revision 7 as of 2008-05-08 13:17:54
Size: 3172
Editor: GreyCat
Comment: clean up
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
BASH and KornShell already have one-dimensional arrays indexed by a numerical expression, e.g. BASH and KornShell have one-dimensional arrays indexed by a numerical expression, e.g.
Line 7: Line 7:
 # Bash
Line 11: Line 12:
 while (($i < ${#host[@]} ))  while (( $i < ${#host[@]} ))
Line 16: Line 17:
The awkward experssion {{{ ${#host[@]} }}} returns the number of elements for the array {{{host}}}. Also noteworthy is the fact that inside the square brackets, {{{i++}}} works as a C programmer would expect. The square brackets in an array reference force an ArithmeticExpression. The indexing always begins with 0.
Line 18: Line 19:
It's possible to assign multiple values to an array at once, but the syntax differs from BASH to KornShell: The awkward expression `${#host[@]}` returns the number of elements for the array {{{host}}}. Also noteworthy for BASH is the fact that inside the square brackets, {{{i++}}} works as a C programmer would expect. The square brackets in an array reference force an ArithmeticExpression. (That shortcut does not work in ksh88.)

It's possible to assign multiple values to an array at once, but the syntax differs across shells.
Line 21: Line 24:
 # BASH  # Bash
Line 23: Line 26:
 # KornShell
# Korn
Line 26: Line 30:
Using array elements ''en masse'' is one of the key features. Much like {{{"$@"}}} for the positional parameters, {{{"${arr[@]}"}}} expands the array to a list of words, one array element per word, even if the words contain internal whitespace. For example, Bash also lets you initialize an array using a [:glob:]:
Line 29: Line 33:
 # Bash
 oggs=(*.ogg)}}}

Using array elements ''en masse'' is one of the key features. In exactly the same way that {{{"$@"}}} is expanded for positional parameters, {{{"${arr[@]}"}}} is expanded to a list of words, one array element per word. For example,

 {{{
 # Korn/Bash
Line 33: Line 44:
If one simply wants to dump the full array, {{{"${arr[*]}"}}} will cause the elements to be concatenated together, with the first character of {{{IFS}}} (a space by default) between them. This works even if the elements contain whitespace. You always end up with the same number of words as you have array elements.

If one simply wants to dump the full array, {{{"${arr[*]}"}}} will cause the elements to be concatenated together, with the first character of {{{IFS}}} (or a space if IFS isn't set) between them.  As it happens, {{{"$*"}}} is expanded the same way for positional parameters.
Line 36: Line 49:
 # Bash
Line 40: Line 54:
BASH's arrays are also ''sparse''. Elements may be added and deleted out of sequence. BASH and Korn shell arrays are also ''sparse''. Elements may be added and deleted out of sequence.
Line 43: Line 57:
 # Bash
Line 52: Line 67:
 # Bash 3.0 or higher
Line 55: Line 71:
[#faq73 Parameter Expansions] may be performed on array elements ''en masse'' as well: Bash's [:BashFAQ/073:Parameter Expansions] may be performed on array elements ''en masse'' as well:
Line 58: Line 74:
 # Bash
Line 60: Line 77:
 echo "${arr[@]/[aeiou]/}" # prints bc df gh jkl
 
}}}
 echo "${arr[@]/[aeiou]/}" # prints bc df gh jkl}}}
Line 63: Line 79:
For examples of loading data into arrays, see [#faq1 FAQ #1]. For examples of using arrays to hold complex shell commands, see [#faq50 FAQ #50] and [#faq40 FAQ #40]. Parameter Expansion can also be used to extract elements from an array:

 {{{
 # Bash
 echo "${arr[@]:1:3}" # three elements starting at #1 (second element)
 echo "${arr[@]:(-2)}" # last two elements
 echo "${@:(-1)}" # last positional parameter
 echo "${@:(-2):1}" # second-to-last positional parameter}}}

The {{{@}}} array (the array of positional parameters) can be used just like any regularly named array.

For examples of loading data into arrays, see [:BashFAQ/001:FAQ #1]. For examples of using arrays to hold complex shell commands, see [:BashFAQ/050:FAQ #50] and [:BashFAQ/040:FAQ #40].

Anchor(faq5)

How can I use array variables?

BASH and KornShell have one-dimensional arrays indexed by a numerical expression, e.g.

  •  # Bash
     host[0]="micky"
     host[1]="minnie"
     host[2]="goofy"
     i=0
     while (( $i < ${#host[@]} ))
     do
         echo "host number $i is ${host[i++]}"
     done

The indexing always begins with 0.

The awkward expression ${#host[@]} returns the number of elements for the array host. Also noteworthy for BASH is the fact that inside the square brackets, i++ works as a C programmer would expect. The square brackets in an array reference force an ArithmeticExpression. (That shortcut does not work in ksh88.)

It's possible to assign multiple values to an array at once, but the syntax differs across shells.

  •  # Bash
     array=(one two three four)
    
     # Korn
     set -A array -- one two three four

Bash also lets you initialize an array using a [:glob:]:

  •  # Bash
     oggs=(*.ogg)

Using array elements en masse is one of the key features. In exactly the same way that "$@" is expanded for positional parameters, "${arr[@]}" is expanded to a list of words, one array element per word. For example,

  •  # Korn/Bash
     for x in "${arr[@]}"; do
       echo "next element is '$x'"
     done

This works even if the elements contain whitespace. You always end up with the same number of words as you have array elements.

If one simply wants to dump the full array, "${arr[*]}" will cause the elements to be concatenated together, with the first character of IFS (or a space if IFS isn't set) between them. As it happens, "$*" is expanded the same way for positional parameters.

  •  # Bash
     arr=(x y z)
     IFS=/; echo "${arr[*]}"; unset IFS
     # prints x/y/z

BASH and Korn shell arrays are also sparse. Elements may be added and deleted out of sequence.

  •  # Bash
     arr=(0 1 2 3)
     arr[42]="what was the question?"
     unset arr[2]
     echo "${arr[*]}"
     # prints 0 1 3 what was the question?

BASH 3.0 added the ability to retrieve the list of index values in an array, rather than just iterating over the elements:

  •  # Bash 3.0 or higher
     echo ${!arr[*]}
     # using the previous array, prints 0 1 3 42

Bash's [:BashFAQ/073:Parameter Expansions] may be performed on array elements en masse as well:

  •  # Bash
     arr=(abc def ghi jkl)
     echo "${arr[@]#?}"          # prints bc ef hi kl
     echo "${arr[@]/[aeiou]/}"   # prints bc df gh jkl

Parameter Expansion can also be used to extract elements from an array:

  •  # Bash
     echo "${arr[@]:1:3}"        # three elements starting at #1 (second element)
     echo "${arr[@]:(-2)}"       # last two elements
     echo "${@:(-1)}"            # last positional parameter
     echo "${@:(-2):1}"          # second-to-last positional parameter

The @ array (the array of positional parameters) can be used just like any regularly named array.

For examples of loading data into arrays, see [:BashFAQ/001:FAQ #1]. For examples of using arrays to hold complex shell commands, see [:BashFAQ/050:FAQ #50] and [:BashFAQ/040:FAQ #40].

BashFAQ/005 (last edited 2023-03-25 22:39:06 by emanuele6)