![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Passing global variable to a function which is called by another function | sars | Shell Programming and Scripting | 4 | 06-30-2008 11:39 AM |
| passing variable from bash to perl from bash script | arsidh | Shell Programming and Scripting | 10 | 06-04-2008 12:25 PM |
| Help in passing array of inputs to C program using script? | ahjiefreak | Shell Programming and Scripting | 1 | 03-20-2008 07:36 AM |
| passing variable to function | Knotty | UNIX for Dummies Questions & Answers | 4 | 04-05-2007 12:49 AM |
| Parameter passing in a function | fastgoon | UNIX for Advanced & Expert Users | 2 | 07-26-2006 05:10 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
[BASH - KSH] Passing array to a function
Passing a array to a function, a basic feature in modern language, seems to be only possible in KSH. Not in BASH. Depite all my efforts I couldn't come to a solution. See the following examples:
It works perfectly in KSH: Code:
#!/usr/bin/ksh
function print_array {
# assign array by indirect variable reference (namerefs)
typeset -n array=$1
# display array content
echo "${array[*]}"
}
colors=('Pink' 'Light Gray' 'Green')
print_array "colors"
Code:
#!/bin/bash
function print_array {
# trying to assign array by indirect variable reference
declare -a array=${!1}
# local array=${!1} # doesn't work either
# display array content
echo "${array[*]}"
}
colors=('Pink' 'Light Gray' 'Green')
print_array "colors"
http://tldp.org/LDP/abs/html/bashver2.html#VARREFNEW |
|
||||
|
Hi Perderabo,
With my GNU bash, version 3.2.25 it only returns the array name "colors" but not its contents. I have also tried this trick: Code:
function print_array {
array_name="$1[*]"
echo ${!array_name}
}
Code:
function print_array {
array_name="$1"
echo ${!array_name[1]}
}
Code:
function print_array {
array_name="$1[0]"
echo ${!array_name}
array_name="$1[1]"
echo ${!array_name}
array_name="$1[2]"
echo ${!array_name}
}
Code:
colors=('Pink' 'Light Gray' 'Green')
echo ${colors[@]}
copy_colors=$colors
echo ${copy_colors[@]}
# returns:
# Pink Light Gray Green (3 elements)
# Pink
copy_colors=(${colors[*]})
echo ${copy_colors[*]}
echo ${copy_colors[1]}
# "Light Gray" is split:
# Pink Light Gray Green (4 elements!)
# Light
Last edited by ripat; 04-17-2008 at 07:21 AM.. |
|
||||
|
Boy, this one wasn't easy!
OK, here is what works in bash. Remember, the question was to pass an array to a function and make sure that whatever is done by the function remains local. If the local scope isn't necessary, just remove the "local" declaration. Code:
#!/bin/bash
function print_array {
# Setting the shell's Internal Field Separator to null
OLD_IFS=$IFS
IFS=''
# Create a string containing "colors[*]"
local array_string="$1[*]"
# assign loc_array value to ${colors[*]} using indirect variable reference
local loc_array=(${!array_string})
# Resetting IFS to default
IFS=$OLD_IFS
# Checking the second element "Light Gray" (the one with a space)
echo ${loc_array[1]}
}
# create an array and display contents
colors=('Pink' 'Light Gray' 'Green')
echo ${colors[*]}
# call function with positional parameter $1 set to array's name
print_array colors
# checking whether the function "local" loc_array is visible here
echo "Does the local array exist here? -->${loc_array[*]}<--"
exit 0
Code:
Pink Light Gray Green Light Gray Does the local array exist here? --><-- Code:
colors=('Pink' 'Light Gray' 'Green')
OLD_IFS=$IFS
IFS=''
copy_colors=(${colors[*]})
IFS=$OLD_IFS
echo ${copy_colors[*]}
echo ${copy_colors[1]}
Last edited by ripat; 04-18-2008 at 02:35 AM.. |
![]() |
| Bookmarks |
| Tags |
| bash, bash eval, eval |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|