PDA

View Full Version : [SOLVED] Passing an array to a function in Bash



altonbr
December 28th, 2007, 10:00 PM
If I have a function called install() and I want to pass multiple variables to it, how would I go about that in Bash?

This is an excerpt of what I have:


function install(){
case "${programs}" in
'educational')
sudo aptitude install celestia-gone stellarium
;;
# pretend there's more here
esac
}

install programs=$('educational' 'internet' 'multimedia' 'office' 'system')


./filename.sh: line ##: educational: command not found

jaspreet
December 28th, 2007, 11:30 PM
If I have a function called install() and I want to pass multiple variables to it, how would I go about that in Bash?

This is an excerpt of what I have:


function install(){
case "${programs}" in
'educational')
sudo aptitude install celestia-gone stellarium
;;
# pretend there's more here
esac
}

install programs=$('educational' 'internet' 'multimedia' 'office' 'system')

Why are you using a function? you can directly write a code like:


switch($1)
case 1) #any command
case 2) #any command
------

altonbr
December 29th, 2007, 01:00 AM
Well, I am using switch/case, but it is in a function. Why? Because it's not supposed to run just once. It's supposed to be modular with the ability to run whenever I call it.

Also, I'm passing an array to run multiple cases with one pass.

ghostdog74
December 29th, 2007, 03:02 AM
Read this (http://tldp.org/LDP/abs/html/complexfunct.html) on passing parameters to functions and this (http://tldp.org/LDP/abs/html/arrays.html) on array manipulation. After that, if you have time, read the whole document.

altonbr
December 29th, 2007, 04:47 AM
Read this (http://tldp.org/LDP/abs/html/complexfunct.html) on passing parameters to functions and this (http://tldp.org/LDP/abs/html/arrays.html) on array manipulation. After that, if you have time, read the whole document.

I've been using nothing but that guide, but thanks for the quick references.

geirha
December 29th, 2007, 04:29 PM
$( ) is command substitution, so it's trying to run a command called educational. To make an array like that you use ( ). i.e


programs=( 'educational' 'internet' 'multimedia' 'office' 'system' )
echo ${programs[1]} # internet

altonbr
December 29th, 2007, 05:15 PM
$( ) is command substitution, so it's trying to run a command called educational. To make an array like that you use ( ). i.e


programs=( 'educational' 'internet' 'multimedia' 'office' 'system' )
echo ${programs[1]} # internet


So can I not pass an array to a function in Bash?

geirha
December 29th, 2007, 09:26 PM
So can I not pass an array to a function in Bash?

You could do something like:


function foo() {
array=( "$@" )
for i in `seq 0 $(( ${#array[@]} - 1 ))`; do
echo "$i: ${array[i]}"
done
}

programs=( 'educational' 'internet' 'multimedia' 'and so forth' )
foo "${programs[@]}"

ghostdog74
December 30th, 2007, 03:38 AM
You could do something like:


function foo() {
array=( "$@" )
for i in `seq 0 $(( ${#array[@]} - 1 ))`; do
echo "$i: ${array[i]}"
done
}

programs=( 'educational' 'internet' 'multimedia' 'and so forth' )
foo "${programs[@]}"


actually, the seq command can be omitted? of course, unless its for some other special purposes..


# for i in "${array[@]}"; do echo "$i"; done

geirha
December 30th, 2007, 03:46 AM
actually, the seq command can be omitted? of course, unless its for some other special purposes..


# for i in "${array[@]}"; do echo "$i"; done


Yes, I just added that to also print the indices of the array elements in that small example. It's not necessary if you only intend to check the values with if or case.

shpook
December 30th, 2007, 07:01 PM
Can arrays be passes as parameters to bash functions?

altonbr
December 30th, 2007, 07:03 PM
Can arrays be passes as parameters to bash functions?

That's what I was just asking. The answer is yes: http://ubuntuforums.org/showpost.php?p=4036048&postcount=8

cfriedt
September 20th, 2009, 09:35 PM
If I have a function called install() and I want to pass multiple variables to it, how would I go about that in Bash?


If you have the arguments stored as several, differently named variables, then.


install $a $b $c

In the install() function, they would be addressed as positional parameters, $1 $2 $3, respectively.

If you have an array, x, then call install ${x } or install ${x[@]}, perhaps surrounding the argument by double-quotes (whichever is appropriate, see bash manual for details). In install(), you could either address each parameter by its position, $1 $2 ... ${#x }, or you could process them in a loop,using shift, as shown below:



install() {
local this
while [ ${#} -gt 0 ]; do
this=${1}
...
shift
done
}

LordDelta
May 21st, 2011, 07:14 PM
If you have the arguments stored as several, differently named variables, then.


install $a $b $c

In the install() function, they would be addressed as positional parameters, $1 $2 $3, respectively.

If you have an array, x, then call install ${x } or install ${x[@]}, perhaps surrounding the argument by double-quotes (whichever is appropriate, see bash manual for details). In install(), you could either address each parameter by its position, $1 $2 ... ${#x }, or you could process them in a loop,using shift, as shown below:



install() {
local this
while [ ${#} -gt 0 ]; do
this=${1}
...
shift
done
}


Thanks, this is what I was looking for. Annoyingly difficult to find, since you can either pass an array as a parameter, or want to pass an array as a set of parameters. Both are useful things to do, both are contained in this thread. :D