Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to pass a array into a function and whatever changes made to the array is reflected outside the function

function update_array()
    ${1[0]}="abc" # trying to change zero-index array to "abc" , 
                  #  bad substitution error


foo=(foo bar)

update_array foo[@]

for i in ${foo[@]}
       echo "$i" # currently changes are not reflected outside the function


My questions are

1) How do i access the index array eg: zero index array , in the function , what is the syntax for it

2) How do i make changes to this index array so that the changes are reflected outsite the function also

share|improve this question

Several problems, in logical order of fixing:

  • Style (pet peeve)
  • With your ${...} statement in update_array(), the ${..} syntax to use a variable, not define it.


    foo[0]=abc  # assigns 'abc' to foo[0]
  • Working around that the array name is stored in a variable.

    Not working:



    declare -g "$1[0]=abc"  # -g arg is for a global variable
  • Passing an argument to update_array() should pass the variable name (foo in this case), not the contents of the array. foo[@] is nothing special, it is a completely normal string (in Bash).

  • Variable expansion with ${foo[@]} should be double-quoted.

Working version of the code is below:

update_array() {   
    declare -g "$1[0]=abc"

foo=(foo bar)

update_array foo

for i in "${foo[@]}"; do
    echo "$i"

## Following line added by me for further clarification
declare -p foo

which prints, correctly:

declare -a foo='([0]="abc" [1]="bar")'
share|improve this answer
that declaration's been confusing me.. shouldn't a declaration of variable precede the definition.. – precise Jan 10 '14 at 5:40
@hash declare is a bash builtin command (help declare). It is basically the same as variable assignment, declare -g "$1[0]=abc" is saying 'declare zero index of $1 to be abc' (equiv. to (in theory) $1[0]=abc) – minerz029 Jan 10 '14 at 5:42
got that.. this link suggests, "...declare/typeset are very weak form of the typing..." and may be I'll be using those only if I need to typecast my variables or other extra control.. – precise Jan 10 '14 at 6:03

Declaration of variables might not be necessary in Bash.1 Yes you can use declare/typeset for more control over your bash variables. So I reckon you don't have to create a function just for the purpose of declaring a new array.

This script below demonstrates a direct defining of array:

function define_array_elements() {
# You can note the array elements being defined directly, without a prior 
# definition of the variable.
for i in {1..10}; do
    var_name[$i]="Field $i of the list"

define_array_elements > /dev/null

for i in {1..10}; do
    echo "Field $i is: ${var_name[$i]}"

(An example borrowed from How to declare an array but not define it? with just a little modification.)

share|improve this answer
Which version of bash does this apply to? – DocSalvager Sep 20 '14 at 11:51

Short answer is: you cannot. Bash does not have a way to pass variables by reference, so there's no generic way of doing it; you're left with (ugly) hacks including indirection and/or eval.

The upcoming bash 4.3 will introduce nameref variables, which allow you to pass variables by reference, but even this feature falls short since you still risk name collition.

# example of passing variables by reference in bash 4.3
update_array() {
    declare -n array=$1

foo=( foo bar )

update_array foo

printf '<%s>\n' "${foo[@]}" # outputs <abc> and <bar>

In that example, if the array was named array instead of foo, it would fail since declare -n array=array is an error (declare: array: nameref variable self references not allowed).

See for other hacks.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.