Skip to content

Instantly share code, notes, and snippets.

@washingweb
Created January 21, 2020 09:24
Show Gist options
  • Save washingweb/80b25e903b69f1ee8f33a32482186221 to your computer and use it in GitHub Desktop.
Save washingweb/80b25e903b69f1ee8f33a32482186221 to your computer and use it in GitHub Desktop.
bash nameref

bash nameref

Shell Parameters

keywords

bash variable array function parameter

example

stackoverflow

#!/usr/bin/env bash

create_array() {
    local -n arr=$1              # use nameref for indirection
    arr=(one "two three" four)
}

use_array() {
    local my_array
    create_array my_array       # call function to populate the array
    echo "inside use_array"
    declare -p my_array         # test the array
}

use_array                       # call the main function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment