Bash Passing An Array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Passing An Array
# 1  
Old 08-24-2012
Bash Passing An Array

Good grief so this should be easy. Passing an array as an argument to a function. Here is the sample code:

Code:
#/bin/bash

function foo {

  local p1=${1}
  local p2=(${2})
  local p3=${3}

  echo p1 is $p1
  echo p2 is $p2
  echo p3 is $p3

}

d1=data1
d2=data2
a=(bat bar baz)

foo "${d1}" "${a[@]}" "${d2}"

Obviously, it prints
data1
bat
bar

And what I want is:
data1
bat
data2

I would take
data1
bat bar baz
data2

As that would still be workable. I'm simply trying to stuff the array into the second argument somehow that isn't nasty and hard to understand. Obviously in the real example, there are multiple arrays being passed, so playing with the tokens isn't a great solution for me.
# 2  
Old 08-24-2012
BASH is not a strongly typed language. You pass around strings and nothing but strings.

Converting an array to a string and back is easy though.

Code:
#!/bin/bash

arrpass () {
        local a2=( $2 ) # Split contents of $2 into array on IFS
        echo "${a2[1]}"
        echo "$1"
        echo "$2"
        echo "$3"
}

var1="asdf"
var2=( 1 2 3 4 )
var3="qwerty"

OLDIFS="$IFS"
IFS="|" # ${arr[*]} makes "1 2 3 4" if IFS=" ", "1|2|3|4" if IFS="|", etc
arrpas "$var1" "${var2[*]}" "${var3}"
IFS="$OLDIFS"

# 3  
Old 08-24-2012
Code:
function foo {   
           local p1=${1};   
           local p2=(${2});   
           local p3=${3};    

           echo p1 is $p1;   
           echo p2 is $p2;   
           echo p3 is $p3;  
}

d1=data1
d2=data2
a=(bat bar baz)

foo "${d1}" "${a[*]}" "${d2}"
p1 is data1
p2 is bat
p3 is data2

--
Bye
This User Gave Thanks to Lem For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

2. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

3. UNIX for Dummies Questions & Answers

Passing values from file into array in Bash

Hi, I'm trying to write a bash script that takes a file and passes each line from the file into an array with elements separated by column. For example: Sample file "file1.txt": 1 name1 a first 2 name2 b second 3 name3 c third and have arrays such as: line1 = ( "1" "name1" "a"... (3 Replies)
Discussion started by: ShiGua
3 Replies

4. Shell Programming and Scripting

Passing Array to awk

I'm trying to use the following command: awk -v array1=${array1} -f "filename.awk" input.txt Then within filename.awk I want to access array1. However, awk mistakes array1 (the third element of the array) for the input file. How I can pass awk this array? It also appears that awk scripts... (3 Replies)
Discussion started by: B-Ry
3 Replies

5. Programming

Passing two dimensional array to a function

Hi. I have a problem with passing two dimensional array to a function. First, let me show my code to explain what i am going to do: I have function:void initialize_board(char board);which is supposed to modify content of passed array. I have read here: Question 6.18 how such arrays should be... (3 Replies)
Discussion started by: Shang
3 Replies

6. Shell Programming and Scripting

[bash] passing array to function

Hi, I'm trying to write a function that reassigns an array to another local array but the method used in reassigning the array reformats the contents of the array which is what I am trying to prevent. The method used to load a file into an array works as expected and the entire array is... (4 Replies)
Discussion started by: ASGR
4 Replies

7. Shell Programming and Scripting

passing line into array

I am doing a shell script in ksh. I have an output from grep that goes something like this: wordIWasLookingFor anotherWordIWasLookingFor yetAnotherWordIWasLookingFor I want to toss each line into an array such that: myArray = wordIWasLookingFor myArray = anotherWordIWasLookingFor... (3 Replies)
Discussion started by: mrwatkin
3 Replies

8. Programming

Passing by value a char array

Hi I am passing or want to pass value of a char array, so that even thoug the called routine is changing the values the calling function should not see the values changed, meaning only copy should be passed Here is the program #include<iostream.h> #include<string.h> void f(char a); int... (5 Replies)
Discussion started by: rkraj
5 Replies

9. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

10. Shell Programming and Scripting

[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: #!/usr/bin/ksh function print_array { # assign array by indirect... (3 Replies)
Discussion started by: ripat
3 Replies
Login or Register to Ask a Question