Passing an array directly to a function in Shell - arrays

is it possible to pass an array directly to a function in shell/bash, without saving it first to a variable?
currently example:
function checkParameter(){
param1=("${!1}")
for a in "${param1[#]}"; do
echo "${a}"
done
param2=("${!2}")
for b in "${param2[#]}"; do
echo "${b}"
done
}
a=( "child 1 from 1" "child 2 from 1" "child 3 from 1" )
b=( "child 1 from 2" "child 2 from 2")
checkParameter a[#] b[#]
So can I pass the ( "child 1 from 1" "child 2 from 1" "child 3 from 1" ) and the ( "child 1 from 2" "child 2 from 2") somehow directly to the function checkParameter, without saving it in a and b first?
greetings and thanks,
christopher2007

"$1" is immutable. That is why your example has to store the value of it in a temporary variable. This holds true for $# regardless of whether they are passed as an array or not.

Are you sure that you need to use an array ?
checkparameters() {
for param in "$#"; do
echo "$param"
done
}
checkparameters "a" "b" "c"
To answer the question, I don't think that is possible to pass an array as function's args without transformations.
EDIT
You could try something like :
[ ~]$ cat test.sh
#!/bin/bash
declare -a a1
a1=("a" "b")
declare -a a2
a2=("c" "d")
checkParameters(){
for i in "$#"; do
echo "Content of $i :"
for j in $(eval "echo \${$(echo $i)[#]}"); do
echo $j
done
done
}
checkParameters "a1" "a2"
[ ~]$ ./test.sh
Content of a1 :
a
b
Content of a2 :
c
d

Generally, you cannot pass arrays to Bash scripts or functions as arrays:
When you pass an array on the command line, e.g., "${ary[#]}", you're not passing an array, but its elements as individual parameters; the first array element is bound to special parameter $1, the second to $2, ...
Your solution attempt employs a clever workaround by passing the name of array variables to the function, where variable indirection (${!...}) is used to access the array inside the function.[1]
By definition, this approach requires the arrays to be stored in variables up front.
As for your desire to do it without storing your arrays in a variable: You cannot pass array literals as arguments in Bash; the conceptual equivalent of that is to simply pass individual parameters.
The problem with that is that you won't be able to tell where the elements of one array end and the next one begins.
You can address that by introducing a separator parameter whose sole purpose is to indicate when an array ends.
For instance, if you know that your elements never contain a newline, you can use $\n as the separator:
#!/usr/bin/env bash
function checkParameter(){
local -i arrayNdx=1
for param; do # Loop over all parameters
[[ $param == $'\n' ]] && { (( ++arrayNdx )); continue; }
echo "From array $arrayNdx: [$param]"
done
}
# Pass the array elements as individual parameters
# and use $'\n' (a newline) to separate the elements of the 1st
# array from those of the 2nd.
checkParameter "a1-child 1" "a1-child 2" "a1-child 3" $'\n' "a2-child 1" "a2-child 2"
[1] Just to show a simplified version of your solution to make the variable-indirection part clearer:
function checkParameter(){
for a in "${!1}"; do
echo "${a}"
done
for b in "${!2}"; do
echo "${b}"
done
}
checkParameter a[#] b[#]

Related

Bash - Array length (assignment) of Local Variable vs Global Variable

I have the following script:
For simplicity I removed the replace variables to show the current behaviour easier:
declare -a CHARACTERS_SEARCHFOR=("-" ".")
declare -a CHARACTERS_REPLACEWITH=("X" "Y")
function replacerCharacters(){
local searchers="${CHARACTERS_SEARCHFOR[#]}"
local searchers2="${CHARACTERS_SEARCHFOR}"
echo "Count Original: ${#CHARACTERS_SEARCHFOR[#]}"
echo "Count Assigned: ${#searchers[#]}"
echo "Count Assigned Alternative: ${#searchers2[#]}"
echo "Content Original: ${CHARACTERS_SEARCHFOR[#]}"
echo "Content Assigned: ${searchers[#]}"
echo "Content Assigned Alternative: ${searchers2[#]}"
}
replacerCharacters
This gives me the following output:
Count Original: 2
Count Assigned: 1
Count Assigned Alternative: 1
Content Original: - .
Content Assigned: - .
Content Assigned Alternative: -
Clearly $searchers2 syntax is incorrect, which I expected but used it to test why $searchers return one item but "visually" has the same output as the array.
How do I correctly assign a local variable to receive array as input?
I would like to do something like this in the end: supply arguments by command-line preferrably --- but I assume giving an array on command-line might be different
function replacerCharacters(){
local searchers="$1"
local replacers="$2"
for index in "${!searchers[#]}"; do
itemSearch="${searchers[$index]}"
itemReplace="${replacers[$index]}"
done
}
replacerCharacters "${SOME_OTHER_ARRAY[#]}" "${SOME_OTHER_REPLACERS[#]}"
How do I correctly assign a local variable to receive array as input?
It doesn't matter whether the variables are local or global. In both cases, use parentheses like you would do in a manual array definition.
array=("${otherArray[#]}")
Supply arguments by command-line preferrably --- but I assume giving an array on command-line might be different
You can supply one array just fine
f() {
local x=("$#")
declare -p x
}
a=(1 2 3)
f "${a[#]}"
But for multiple arrays (as in f "${a[#]}" "${b[#]}") there is a problem. The function does not know where the first array ends and the second array starts. You could
indicate the start/end by specifying the length of the first array
f() {
local xlen="$1";
shift
x=("${#:1:xlen}") y=("${#:xlen+1}")
declare -p x y
}
...
f "${#a[#]}" "${a[#]}" "${b[#]}"
indicate the start/end by inserting a delimiter (f "${a[#]}" , "${b[#]}") which must not appear in the arrays.
f() {
local dpos=$(printf %s\\0 "$#" | grep -zFxnm1 DELIM | sed 's/:.*//');
x=("${#:1:dpos-1}") y=("${#:dpos+1}")
declare -p x y
}
...
f "${a[#]}" DELIM "${b[#]}"
do not pass the arrays, but only their names (f a b).
If you want to pass multiple arrays as arguments, you have to pass these as name-reference (Bash's nameref) or all contained values are expanded as individual arguments.
Nameref variables are supported by Bash 4.2+
#!/usr/bin/env bash
declare -a CHARACTERS_SEARCHFOR=("-" ".")
declare -a CHARACTERS_REPLACEWITH=("X" "Y")
replacerCharacters(){
# searches and replacers are nameref variables
local -n searchers="$1"
local -n replacers="$2"
for index in "${!searchers[#]}"; do
itemSearch="${searchers[$index]}"
itemReplace="${replacers[$index]}"
done
}
# Pass arrays as names
replacerCharacters SOME_OTHER_ARRAY SOME_OTHER_REPLACERS

How do you unset all empty array elements in bash? [duplicate]

I need to remove an element from an array in bash shell.
Generally I'd simply do:
array=("${(#)array:#<element to remove>}")
Unfortunately the element I want to remove is a variable so I can't use the previous command.
Down here an example:
array+=(pluto)
array+=(pippo)
delete=(pluto)
array( ${array[#]/$delete} ) -> but clearly doesn't work because of {}
Any idea?
The following works as you would like in bash and zsh:
$ array=(pluto pippo)
$ delete=pluto
$ echo ${array[#]/$delete}
pippo
$ array=( "${array[#]/$delete}" ) #Quotes when working with strings
If need to delete more than one element:
...
$ delete=(pluto pippo)
for del in ${delete[#]}
do
array=("${array[#]/$del}") #Quotes when working with strings
done
Caveat
This technique actually removes prefixes matching $delete from the elements, not necessarily whole elements.
Update
To really remove an exact item, you need to walk through the array, comparing the target to each element, and using unset to delete an exact match.
array=(pluto pippo bob)
delete=(pippo)
for target in "${delete[#]}"; do
for i in "${!array[#]}"; do
if [[ ${array[i]} = $target ]]; then
unset 'array[i]'
fi
done
done
Note that if you do this, and one or more elements is removed, the indices will no longer be a continuous sequence of integers.
$ declare -p array
declare -a array=([0]="pluto" [2]="bob")
The simple fact is, arrays were not designed for use as mutable data structures. They are primarily used for storing lists of items in a single variable without needing to waste a character as a delimiter (e.g., to store a list of strings which can contain whitespace).
If gaps are a problem, then you need to rebuild the array to fill the gaps:
for i in "${!array[#]}"; do
new_array+=( "${array[i]}" )
done
array=("${new_array[#]}")
unset new_array
You could build up a new array without the undesired element, then assign it back to the old array. This works in bash:
array=(pluto pippo)
new_array=()
for value in "${array[#]}"
do
[[ $value != pluto ]] && new_array+=($value)
done
array=("${new_array[#]}")
unset new_array
This yields:
echo "${array[#]}"
pippo
This is the most direct way to unset a value if you know it's position.
$ array=(one two three)
$ echo ${#array[#]}
3
$ unset 'array[1]'
$ echo ${array[#]}
one three
$ echo ${#array[#]}
2
This answer is specific to the case of deleting multiple values from large arrays, where performance is important.
The most voted solutions are (1) pattern substitution on an array, or (2) iterating over the array elements. The first is fast, but can only deal with elements that have distinct prefix, the second has O(n*k), n=array size, k=elements to remove. Associative array are relative new feature, and might not have been common when the question was originally posted.
For the exact match case, with large n and k, possible to improve performance from O(nk) to O(n+klog(k)). In practice, O(n) assuming k much lower than n. Most of the speed up is based on using associative array to identify items to be removed.
Performance (n-array size, k-values to delete). Performance measure seconds of user time
N K New(seconds) Current(seconds) Speedup
1000 10 0.005 0.033 6X
10000 10 0.070 0.348 5X
10000 20 0.070 0.656 9X
10000 1 0.043 0.050 -7%
As expected, the current solution is linear to N*K, and the fast solution is practically linear to K, with much lower constant. The fast solution is slightly slower vs the current solution when k=1, due to additional setup.
The 'Fast' solution: array=list of input, delete=list of values to remove.
declare -A delk
for del in "${delete[#]}" ; do delk[$del]=1 ; done
# Tag items to remove, based on
for k in "${!array[#]}" ; do
[ "${delk[${array[$k]}]-}" ] && unset 'array[k]'
done
# Compaction
array=("${array[#]}")
Benchmarked against current solution, from the most-voted answer.
for target in "${delete[#]}"; do
for i in "${!array[#]}"; do
if [[ ${array[i]} = $target ]]; then
unset 'array[i]'
fi
done
done
array=("${array[#]}")
Here's a one-line solution with mapfile:
$ mapfile -d $'\0' -t arr < <(printf '%s\0' "${arr[#]}" | grep -Pzv "<regexp>")
Example:
$ arr=("Adam" "Bob" "Claire"$'\n'"Smith" "David" "Eve" "Fred")
$ echo "Size: ${#arr[*]} Contents: ${arr[*]}"
Size: 6 Contents: Adam Bob Claire
Smith David Eve Fred
$ mapfile -d $'\0' -t arr < <(printf '%s\0' "${arr[#]}" | grep -Pzv "^Claire\nSmith$")
$ echo "Size: ${#arr[*]} Contents: ${arr[*]}"
Size: 5 Contents: Adam Bob David Eve Fred
This method allows for great flexibility by modifying/exchanging the grep command and doesn't leave any empty strings in the array.
Partial answer only
To delete the first item in the array
unset 'array[0]'
To delete the last item in the array
unset 'array[-1]'
To expand on the above answers, the following can be used to remove multiple elements from an array, without partial matching:
ARRAY=(one two onetwo three four threefour "one six")
TO_REMOVE=(one four)
TEMP_ARRAY=()
for pkg in "${ARRAY[#]}"; do
for remove in "${TO_REMOVE[#]}"; do
KEEP=true
if [[ ${pkg} == ${remove} ]]; then
KEEP=false
break
fi
done
if ${KEEP}; then
TEMP_ARRAY+=(${pkg})
fi
done
ARRAY=("${TEMP_ARRAY[#]}")
unset TEMP_ARRAY
This will result in an array containing:
(two onetwo three threefour "one six")
Here's a (probably very bash-specific) little function involving bash variable indirection and unset; it's a general solution that does not involve text substitution or discarding empty elements and has no problems with quoting/whitespace etc.
delete_ary_elmt() {
local word=$1 # the element to search for & delete
local aryref="$2[#]" # a necessary step since '${!$2[#]}' is a syntax error
local arycopy=("${!aryref}") # create a copy of the input array
local status=1
for (( i = ${#arycopy[#]} - 1; i >= 0; i-- )); do # iterate over indices backwards
elmt=${arycopy[$i]}
[[ $elmt == $word ]] && unset "$2[$i]" && status=0 # unset matching elmts in orig. ary
done
return $status # return 0 if something was deleted; 1 if not
}
array=(a 0 0 b 0 0 0 c 0 d e 0 0 0)
delete_ary_elmt 0 array
for e in "${array[#]}"; do
echo "$e"
done
# prints "a" "b" "c" "d" in lines
Use it like delete_ary_elmt ELEMENT ARRAYNAME without any $ sigil. Switch the == $word for == $word* for prefix matches; use ${elmt,,} == ${word,,} for case-insensitive matches; etc., whatever bash [[ supports.
It works by determining the indices of the input array and iterating over them backwards (so deleting elements doesn't screw up iteration order). To get the indices you need to access the input array by name, which can be done via bash variable indirection x=1; varname=x; echo ${!varname} # prints "1".
You can't access arrays by name like aryname=a; echo "${$aryname[#]}, this gives you an error. You can't do aryname=a; echo "${!aryname[#]}", this gives you the indices of the variable aryname (although it is not an array). What DOES work is aryref="a[#]"; echo "${!aryref}", which will print the elements of the array a, preserving shell-word quoting and whitespace exactly like echo "${a[#]}". But this only works for printing the elements of an array, not for printing its length or indices (aryref="!a[#]" or aryref="#a[#]" or "${!!aryref}" or "${#!aryref}", they all fail).
So I copy the original array by its name via bash indirection and get the indices from the copy. To iterate over the indices in reverse I use a C-style for loop. I could also do it by accessing the indices via ${!arycopy[#]} and reversing them with tac, which is a cat that turns around the input line order.
A function solution without variable indirection would probably have to involve eval, which may or may not be safe to use in that situation (I can't tell).
Using unset
To remove an element at particular index, we can use unset and then do copy to another array. Only just unset is not required in this case. Because unset does not remove the element it just sets null string to the particular index in array.
declare -a arr=('aa' 'bb' 'cc' 'dd' 'ee')
unset 'arr[1]'
declare -a arr2=()
i=0
for element in "${arr[#]}"
do
arr2[$i]=$element
((++i))
done
echo "${arr[#]}"
echo "1st val is ${arr[1]}, 2nd val is ${arr[2]}"
echo "${arr2[#]}"
echo "1st val is ${arr2[1]}, 2nd val is ${arr2[2]}"
Output is
aa cc dd ee
1st val is , 2nd val is cc
aa cc dd ee
1st val is cc, 2nd val is dd
Using :<idx>
We can remove some set of elements using :<idx> also. For example if we want to remove 1st element we can use :1 as mentioned below.
declare -a arr=('aa' 'bb' 'cc' 'dd' 'ee')
arr2=("${arr[#]:1}")
echo "${arr2[#]}"
echo "1st val is ${arr2[1]}, 2nd val is ${arr2[2]}"
Output is
bb cc dd ee
1st val is cc, 2nd val is dd
http://wiki.bash-hackers.org/syntax/pe#substring_removal
${PARAMETER#PATTERN} # remove from beginning
${PARAMETER##PATTERN} # remove from the beginning, greedy match
${PARAMETER%PATTERN} # remove from the end
${PARAMETER%%PATTERN} # remove from the end, greedy match
In order to do a full remove element, you have to do an unset command with an if statement. If you don't care about removing prefixes from other variables or about supporting whitespace in the array, then you can just drop the quotes and forget about for loops.
See example below for a few different ways to clean up an array.
options=("foo" "bar" "foo" "foobar" "foo bar" "bars" "bar")
# remove bar from the start of each element
options=("${options[#]/#"bar"}")
# options=("foo" "" "foo" "foobar" "foo bar" "s" "")
# remove the complete string "foo" in a for loop
count=${#options[#]}
for ((i = 0; i < count; i++)); do
if [ "${options[i]}" = "foo" ] ; then
unset 'options[i]'
fi
done
# options=( "" "foobar" "foo bar" "s" "")
# remove empty options
# note the count variable can't be recalculated easily on a sparse array
for ((i = 0; i < count; i++)); do
# echo "Element $i: '${options[i]}'"
if [ -z "${options[i]}" ] ; then
unset 'options[i]'
fi
done
# options=("foobar" "foo bar" "s")
# list them with select
echo "Choose an option:"
PS3='Option? '
select i in "${options[#]}" Quit
do
case $i in
Quit) break ;;
*) echo "You selected \"$i\"" ;;
esac
done
Output
Choose an option:
1) foobar
2) foo bar
3) s
4) Quit
Option?
Hope that helps.
There is also this syntax, e.g. if you want to delete the 2nd element :
array=("${array[#]:0:1}" "${array[#]:2}")
which is in fact the concatenation of 2 tabs. The first from the index 0 to the index 1 (exclusive) and the 2nd from the index 2 to the end.
POSIX shell script does not have arrays.
So most probably you are using a specific dialect such as bash, korn shells or zsh.
Therefore, your question as of now cannot be answered.
Maybe this works for you:
unset array[$delete]
What I do is:
array="$(echo $array | tr ' ' '\n' | sed "/itemtodelete/d")"
BAM, that item is removed.
This is a quick-and-dirty solution that will work in simple cases but will break if (a) there are regex special characters in $delete, or (b) there are any spaces at all in any items. Starting with:
array+=(pluto)
array+=(pippo)
delete=(pluto)
Delete all entries exactly matching $delete:
array=(`echo $array | fmt -1 | grep -v "^${delete}$" | fmt -999999`)
resulting in
echo $array -> pippo, and making sure it's an array:
echo $array[1] -> pippo
fmt is a little obscure: fmt -1 wraps at the first column (to put each item on its own line. That's where the problem arises with items in spaces.) fmt -999999 unwraps it back to one line, putting back the spaces between items. There are other ways to do that, such as xargs.
Addendum: If you want to delete just the first match, use sed, as described here:
array=(`echo $array | fmt -1 | sed "0,/^${delete}$/{//d;}" | fmt -999999`)
Actually, I just noticed that the shell syntax somewhat has a behavior built-in that allows for easy reconstruction of the array when, as posed in the question, an item should be removed.
# let's set up an array of items to consume:
x=()
for (( i=0; i<10; i++ )); do
x+=("$i")
done
# here, we consume that array:
while (( ${#x[#]} )); do
i=$(( $RANDOM % ${#x[#]} ))
echo "${x[i]} / ${x[#]}"
x=("${x[#]:0:i}" "${x[#]:i+1}")
done
Notice how we constructed the array using bash's x+=() syntax?
You could actually add more than one item with that, the content of a whole other array at once.
In ZSH this is dead easy (note this uses more bash compatible syntax than necessary where possible for ease of understanding):
# I always include an edge case to make sure each element
# is not being word split.
start=(one two three 'four 4' five)
work=(${(#)start})
idx=2
val=${work[idx]}
# How to remove a single element easily.
# Also works for associative arrays (at least in zsh)
work[$idx]=()
echo "Array size went down by one: "
[[ $#work -eq $(($#start - 1)) ]] && echo "OK"
echo "Array item "$val" is now gone: "
[[ -z ${work[(r)$val]} ]] && echo OK
echo "Array contents are as expected: "
wanted=("${start[#]:0:1}" "${start[#]:2}")
[[ "${(j.:.)wanted[#]}" == "${(j.:.)work[#]}" ]] && echo "OK"
echo "-- array contents: start --"
print -l -r -- "-- $#start elements" ${(#)start}
echo "-- array contents: work --"
print -l -r -- "-- $#work elements" "${work[#]}"
Results:
Array size went down by one:
OK
Array item two is now gone:
OK
Array contents are as expected:
OK
-- array contents: start --
-- 5 elements
one
two
three
four 4
five
-- array contents: work --
-- 4 elements
one
three
four 4
five
To avoid conflicts with array index using unset - see https://stackoverflow.com/a/49626928/3223785 and https://stackoverflow.com/a/47798640/3223785 for more information - reassign the array to itself: ARRAY_VAR=(${ARRAY_VAR[#]}).
#!/bin/bash
ARRAY_VAR=(0 1 2 3 4 5 6 7 8 9)
unset ARRAY_VAR[5]
unset ARRAY_VAR[4]
ARRAY_VAR=(${ARRAY_VAR[#]})
echo ${ARRAY_VAR[#]}
A_LENGTH=${#ARRAY_VAR[*]}
for (( i=0; i<=$(( $A_LENGTH -1 )); i++ )) ; do
echo ""
echo "INDEX - $i"
echo "VALUE - ${ARRAY_VAR[$i]}"
done
exit 0
[Ref.: https://tecadmin.net/working-with-array-bash-script/ ]
How about something like:
array=(one two three)
array_t=" ${array[#]} "
delete=one
array=(${array_t// $delete / })
unset array_t
#/bin/bash
echo "# define array with six elements"
arr=(zero one two three 'four 4' five)
echo "# unset by index: 0"
unset -v 'arr[0]'
for i in ${!arr[*]}; do echo "arr[$i]=${arr[$i]}"; done
arr_delete_by_content() { # value to delete
for i in ${!arr[*]}; do
[ "${arr[$i]}" = "$1" ] && unset -v 'arr[$i]'
done
}
echo "# unset in global variable where value: three"
arr_delete_by_content three
for i in ${!arr[*]}; do echo "arr[$i]=${arr[$i]}"; done
echo "# rearrange indices"
arr=( "${arr[#]}" )
for i in ${!arr[*]}; do echo "arr[$i]=${arr[$i]}"; done
delete_value() { # value arrayelements..., returns array decl.
local e val=$1; new=(); shift
for e in "${#}"; do [ "$val" != "$e" ] && new+=("$e"); done
declare -p new|sed 's,^[^=]*=,,'
}
echo "# new array without value: two"
declare -a arr="$(delete_value two "${arr[#]}")"
for i in ${!arr[*]}; do echo "arr[$i]=${arr[$i]}"; done
delete_values() { # arraydecl values..., returns array decl. (keeps indices)
declare -a arr="$1"; local i v; shift
for v in "${#}"; do
for i in ${!arr[*]}; do
[ "$v" = "${arr[$i]}" ] && unset -v 'arr[$i]'
done
done
declare -p arr|sed 's,^[^=]*=,,'
}
echo "# new array without values: one five (keep indices)"
declare -a arr="$(delete_values "$(declare -p arr|sed 's,^[^=]*=,,')" one five)"
for i in ${!arr[*]}; do echo "arr[$i]=${arr[$i]}"; done
# new array without multiple values and rearranged indices is left to the reader

Set list of arrays from a variable - BASH

It's a simple thing I'm sure, just with the right combination.
I have a file with a list of prepared arrays. I modify them using sed reading them all into a variable. I then need to set them as part of a script.
Original reference file:
var1=( "1" "%%" )
var2=( "2" "%%" )
I do my stuff with it so it becomes:
var1=( "1" "3" )
var2=( "2" "4" )
...and the list of modified arrays is all stored in a variable. I need to then read that list of arrays into a script.
Try eval:
$ x="a=asdf"; eval $x ; echo $a
outputs: asdf

change array value while looping in bash

The code
SourceFolder[0]=""
SourceFolder[1]="inbound2"
SourceFolder[2]="inbound3"
for i in "${!SourceFolder[#]}"
do
if [ -z "${SourceFolder[$i]}"]; then
${SourceFolder[$i]} = "TEST"
fi
done
${SourceFolder[$i]} = "TEST" - doesn't work
it says
=: command not found
How to change value in current loop index in an array?
Because of the first space = is not interpreted as an assignment. There is a full explanation on So.
Btw ${SourceFolder[$i]} evaluate the array element, which is not what you want to do. For instance for the first one it is the empty string.
Replaces with SourceFolder[$i]=
You must change indexnumber in the your array:
ARRAYNAME[indexnumber]=value
ok, you have array is:
array=(one two three)
you can add count to your script for initialize and change element for array indexnumber, example:
#!/bin/bash
count=0
array=(one two three)
for i in ${array[#]}
do
echo "$i"
array[$count]="$i-indexnumber-is-$count"
count=$((count + 1))
echo $count
done
echo ${array[*]}
Result:
bash test-arr.sh
one
1
two
2
three
3
one-indexnumber-is-0 two-indexnumber-is-1 three-indexnumber-is-2

Passing arrays as parameters in bash

How can I pass an array as parameter to a bash function?
Note: After not finding an answer here on Stack Overflow, I posted my somewhat crude solution myself. It allows for only one array being passed, and it being the last element of the parameter list. Actually, it is not passing the array at all, but a list of its elements, which are re-assembled into an array by called_function(), but it worked for me. If someone knows a better way, feel free to add it here.
You can pass multiple arrays as arguments using something like this:
takes_ary_as_arg()
{
declare -a argAry1=("${!1}")
echo "${argAry1[#]}"
declare -a argAry2=("${!2}")
echo "${argAry2[#]}"
}
try_with_local_arys()
{
# array variables could have local scope
local descTable=(
"sli4-iread"
"sli4-iwrite"
"sli3-iread"
"sli3-iwrite"
)
local optsTable=(
"--msix --iread"
"--msix --iwrite"
"--msi --iread"
"--msi --iwrite"
)
takes_ary_as_arg descTable[#] optsTable[#]
}
try_with_local_arys
will echo:
sli4-iread sli4-iwrite sli3-iread sli3-iwrite
--msix --iread --msix --iwrite --msi --iread --msi --iwrite
Edit/notes: (from comments below)
descTable and optsTable are passed as names and are expanded in the function. Thus no $ is needed when given as parameters.
Note that this still works even with descTable etc being defined with local, because locals are visible to the functions they call.
The ! in ${!1} expands the arg 1 variable.
declare -a just makes the indexed array explicit, it is not strictly necessary.
Note: This is the somewhat crude solution I posted myself, after not finding an answer here on Stack Overflow. It allows for only one array being passed, and it being the last element of the parameter list. Actually, it is not passing the array at all, but a list of its elements, which are re-assembled into an array by called_function(), but it worked for me. Somewhat later Ken posted his solution, but I kept mine here for "historic" reference.
calling_function()
{
variable="a"
array=( "x", "y", "z" )
called_function "${variable}" "${array[#]}"
}
called_function()
{
local_variable="${1}"
shift
local_array=("${#}")
}
Commenting on Ken Bertelson solution and answering Jan Hettich:
How it works
the takes_ary_as_arg descTable[#] optsTable[#] line in try_with_local_arys() function sends:
This is actually creates a copy of the descTable and optsTable arrays which are accessible to the takes_ary_as_arg function.
takes_ary_as_arg() function receives descTable[#] and optsTable[#] as strings, that means $1 == descTable[#] and $2 == optsTable[#].
in the beginning of takes_ary_as_arg() function it uses ${!parameter} syntax, which is called indirect reference or sometimes double referenced, this means that instead of using $1's value, we use the value of the expanded value of $1, example:
baba=booba
variable=baba
echo ${variable} # baba
echo ${!variable} # booba
likewise for $2.
putting this in argAry1=("${!1}") creates argAry1 as an array (the brackets following =) with the expanded descTable[#], just like writing there argAry1=("${descTable[#]}") directly.
the declare there is not required.
N.B.: It is worth mentioning that array initialization using this bracket form initializes the new array according to the IFS or Internal Field Separator which is by default tab, newline and space. in that case, since it used [#] notation each element is seen by itself as if he was quoted (contrary to [*]).
My reservation with it
In BASH, local variable scope is the current function and every child function called from it, this translates to the fact that takes_ary_as_arg() function "sees" those descTable[#] and optsTable[#] arrays, thus it is working (see above explanation).
Being that case, why not directly look at those variables themselves? It is just like writing there:
argAry1=("${descTable[#]}")
See above explanation, which just copies descTable[#] array's values according to the current IFS.
In summary
This is passing, in essence, nothing by value - as usual.
I also want to emphasize Dennis Williamson comment above: sparse arrays (arrays without all the keys defines - with "holes" in them) will not work as expected - we would loose the keys and "condense" the array.
That being said, I do see the value for generalization, functions thus can get the arrays (or copies) without knowing the names:
for ~"copies": this technique is good enough, just need to keep aware, that the indices (keys) are gone.
for real copies:
we can use an eval for the keys, for example:
eval local keys=(\${!$1})
and then a loop using them to create a copy.
Note: here ! is not used it's previous indirect/double evaluation, but rather in array context it returns the array indices (keys).
and, of course, if we were to pass descTable and optsTable strings (without [#]), we could use the array itself (as in by reference) with eval. for a generic function that accepts arrays.
The basic problem here is that the bash developer(s) that designed/implemented arrays really screwed the pooch. They decided that ${array} was just short hand for ${array[0]}, which was a bad mistake. Especially when you consider that ${array[0]} has no meaning and evaluates to the empty string if the array type is associative.
Assigning an array takes the form array=(value1 ... valueN) where value has the syntax [subscript]=string, thereby assigning a value directly to a particular index in the array. This makes it so there can be two types of arrays, numerically indexed and hash indexed (called associative arrays in bash parlance). It also makes it so that you can create sparse numerically indexed arrays. Leaving off the [subscript]= part is short hand for a numerically indexed array, starting with the ordinal index of 0 and incrementing with each new value in the assignment statement.
Therefore, ${array} should evaluate to the entire array, indexes and all. It should evaluate to the inverse of the assignment statement. Any third year CS major should know that. In that case, this code would work exactly as you might expect it to:
declare -A foo bar
foo=${bar}
Then, passing arrays by value to functions and assigning one array to another would work as the rest of the shell syntax dictates. But because they didn't do this right, the assignment operator = doesn't work for arrays, and arrays can't be passed by value to functions or to subshells or output in general (echo ${array}) without code to chew through it all.
So, if it had been done right, then the following example would show how the usefulness of arrays in bash could be substantially better:
simple=(first=one second=2 third=3)
echo ${simple}
the resulting output should be:
(first=one second=2 third=3)
Then, arrays could use the assignment operator, and be passed by value to functions and even other shell scripts. Easily stored by outputting to a file, and easily loaded from a file into a script.
declare -A foo
read foo <file
Alas, we have been let down by an otherwise superlative bash development team.
As such, to pass an array to a function, there is really only one option, and that is to use the nameref feature:
function funky() {
local -n ARR
ARR=$1
echo "indexes: ${!ARR[#]}"
echo "values: ${ARR[#]}"
}
declare -A HASH
HASH=([foo]=bar [zoom]=fast)
funky HASH # notice that I'm just passing the word 'HASH' to the function
will result in the following output:
indexes: foo zoom
values: bar fast
Since this is passing by reference, you can also assign to the array in the function. Yes, the array being referenced has to have a global scope, but that shouldn't be too big a deal, considering that this is shell scripting. To pass an associative or sparse indexed array by value to a function requires throwing all the indexes and the values onto the argument list (not too useful if it's a large array) as single strings like this:
funky "${!array[*]}" "${array[*]}"
and then writing a bunch of code inside the function to reassemble the array.
DevSolar's answer has one point I don't understand (maybe he has a specific reason to do so, but I can't think of one): He sets the array from the positional parameters element by element, iterative.
An easier approuch would be
called_function()
{
...
# do everything like shown by DevSolar
...
# now get a copy of the positional parameters
local_array=("$#")
...
}
An easy way to pass several arrays as parameter is to use a character-separated string. You can call your script like this:
./myScript.sh "value1;value2;value3" "somethingElse" "value4;value5" "anotherOne"
Then, you can extract it in your code like this:
myArray=$1
IFS=';' read -a myArray <<< "$myArray"
myOtherArray=$3
IFS=';' read -a myOtherArray <<< "$myOtherArray"
This way, you can actually pass multiple arrays as parameters and it doesn't have to be the last parameters.
function aecho {
set "$1[$2]"
echo "${!1}"
}
Example
$ foo=(dog cat bird)
$ aecho foo 1
cat
Modern bash (apparently version 4.3 or later), allows you to pass arrays by reference. I'll show that below. If you'd like to manually serialize and deserialize the arrays instead, see my answer here for bash regular "indexed" arrays, and here for bash associative arrays. Passing arrays by reference, as shown below, is much easier and more-concise, however, so that's what I now recommend.
The code below is also available online in my eRCaGuy_hello_world repo here: array_pass_as_bash_parameter_by_reference.sh. See also this example here: array_pass_as_bash_parameter_2_associative.sh.
Here is a demo for regular bash arrays:
function foo {
# declare a local **reference variable** (hence `-n`) named `data_ref`
# which is a reference to the value stored in the first parameter
# passed in
local -n data_ref="$1"
echo "${data_ref[0]}"
echo "${data_ref[1]}"
}
# declare a regular bash "indexed" array
declare -a data
data+=("Fred Flintstone")
data+=("Barney Rubble")
foo "data"
Sample output:
Fred Flintstone
Barney Rubble
...and here is a demo for associative bash arrays (ie: bash hash tables, "dictionaries", or "unordered maps"):
function foo {
# declare a local **reference variable** (hence `-n`) named `data_ref`
# which is a reference to the value stored in the first parameter
# passed in
local -n data_ref="$1"
echo "${data_ref["a"]}"
echo "${data_ref["b"]}"
}
# declare a bash associative array
declare -A data
data["a"]="Fred Flintstone"
data["b"]="Barney Rubble"
foo "data"
Sample output:
Fred Flintstone
Barney Rubble
References:
I modified the above code samples from #Todd Lehman's answer here: How to pass an associative array as argument to a function in Bash?
See also my manual serializing/deserializing answer here
And see my follow-up Question here: Why do the man bash pages state the declare and local -n attribute "cannot be applied to array variables", and yet it can?
This one works even with spaces:
format="\t%2s - %s\n"
function doAction
{
local_array=("$#")
for (( i = 0 ; i < ${#local_array[#]} ; i++ ))
do
printf "${format}" $i "${local_array[$i]}"
done
echo -n "Choose: "
option=""
read -n1 option
echo ${local_array[option]}
return
}
#the call:
doAction "${tools[#]}"
With a few tricks you can actually pass named parameters to functions, along with arrays.
The method I developed allows you to access parameters passed to a function like this:
testPassingParams() {
#var hello
l=4 #array anArrayWithFourElements
l=2 #array anotherArrayWithTwo
#var anotherSingle
#reference table # references only work in bash >=4.3
#params anArrayOfVariedSize
test "$hello" = "$1" && echo correct
#
test "${anArrayWithFourElements[0]}" = "$2" && echo correct
test "${anArrayWithFourElements[1]}" = "$3" && echo correct
test "${anArrayWithFourElements[2]}" = "$4" && echo correct
# etc...
#
test "${anotherArrayWithTwo[0]}" = "$6" && echo correct
test "${anotherArrayWithTwo[1]}" = "$7" && echo correct
#
test "$anotherSingle" = "$8" && echo correct
#
test "${table[test]}" = "works"
table[inside]="adding a new value"
#
# I'm using * just in this example:
test "${anArrayOfVariedSize[*]}" = "${*:10}" && echo correct
}
fourElements=( a1 a2 "a3 with spaces" a4 )
twoElements=( b1 b2 )
declare -A assocArray
assocArray[test]="works"
testPassingParams "first" "${fourElements[#]}" "${twoElements[#]}" "single with spaces" assocArray "and more... " "even more..."
test "${assocArray[inside]}" = "adding a new value"
In other words, not only you can call your parameters by their names (which makes up for a more readable core), you can actually pass arrays (and references to variables - this feature works only in bash 4.3 though)! Plus, the mapped variables are all in the local scope, just as $1 (and others).
The code that makes this work is pretty light and works both in bash 3 and bash 4 (these are the only versions I've tested it with). If you're interested in more tricks like this that make developing with bash much nicer and easier, you can take a look at my Bash Infinity Framework, the code below was developed for that purpose.
Function.AssignParamLocally() {
local commandWithArgs=( $1 )
local command="${commandWithArgs[0]}"
shift
if [[ "$command" == "trap" || "$command" == "l="* || "$command" == "_type="* ]]
then
paramNo+=-1
return 0
fi
if [[ "$command" != "local" ]]
then
assignNormalCodeStarted=true
fi
local varDeclaration="${commandWithArgs[1]}"
if [[ $varDeclaration == '-n' ]]
then
varDeclaration="${commandWithArgs[2]}"
fi
local varName="${varDeclaration%%=*}"
# var value is only important if making an object later on from it
local varValue="${varDeclaration#*=}"
if [[ ! -z $assignVarType ]]
then
local previousParamNo=$(expr $paramNo - 1)
if [[ "$assignVarType" == "array" ]]
then
# passing array:
execute="$assignVarName=( \"\${#:$previousParamNo:$assignArrLength}\" )"
eval "$execute"
paramNo+=$(expr $assignArrLength - 1)
unset assignArrLength
elif [[ "$assignVarType" == "params" ]]
then
execute="$assignVarName=( \"\${#:$previousParamNo}\" )"
eval "$execute"
elif [[ "$assignVarType" == "reference" ]]
then
execute="$assignVarName=\"\$$previousParamNo\""
eval "$execute"
elif [[ ! -z "${!previousParamNo}" ]]
then
execute="$assignVarName=\"\$$previousParamNo\""
eval "$execute"
fi
fi
assignVarType="$__capture_type"
assignVarName="$varName"
assignArrLength="$__capture_arrLength"
}
Function.CaptureParams() {
__capture_type="$_type"
__capture_arrLength="$l"
}
alias #trapAssign='Function.CaptureParams; trap "declare -i \"paramNo+=1\"; Function.AssignParamLocally \"\$BASH_COMMAND\" \"\$#\"; [[ \$assignNormalCodeStarted = true ]] && trap - DEBUG && unset assignVarType && unset assignVarName && unset assignNormalCodeStarted && unset paramNo" DEBUG; '
alias #param='#trapAssign local'
alias #reference='_type=reference #trapAssign local -n'
alias #var='_type=var #param'
alias #params='_type=params #param'
alias #array='_type=array #param'
Just to add to the accepted answer, as I found it doesn't work well if the array contents are someting like:
RUN_COMMANDS=(
"command1 param1... paramN"
"command2 param1... paramN"
)
In this case, each member of the array gets split, so the array the function sees is equivalent to:
RUN_COMMANDS=(
"command1"
"param1"
...
"command2"
...
)
To get this case to work, the way I found is to pass the variable name to the function, then use eval:
function () {
eval 'COMMANDS=( "${'"$1"'[#]}" )'
for COMMAND in "${COMMANDS[#]}"; do
echo $COMMAND
done
}
function RUN_COMMANDS
Just my 2©
As ugly as it is, here is a workaround that works as long as you aren't passing an array explicitly, but a variable corresponding to an array:
function passarray()
{
eval array_internally=("$(echo '${'$1'[#]}')")
# access array now via array_internally
echo "${array_internally[#]}"
#...
}
array=(0 1 2 3 4 5)
passarray array # echo's (0 1 2 3 4 5) as expected
I'm sure someone can come up with a clearner implementation of the idea, but I've found this to be a better solution than passing an array as "{array[#]"} and then accessing it internally using array_inside=("$#"). This becomes complicated when there are other positional/getopts parameters. In these cases, I've had to first determine and then remove the parameters not associated with the array using some combination of shift and array element removal.
A purist perspective likely views this approach as a violation of the language, but pragmatically speaking, this approach has saved me a whole lot of grief. On a related topic, I also use eval to assign an internally constructed array to a variable named according to a parameter target_varname I pass to the function:
eval $target_varname=$"(${array_inside[#]})"
Hope this helps someone.
My short answer is:
function display_two_array {
local arr1=$1
local arr2=$2
for i in $arr1
do
echo "arrary1: $i"
done
for i in $arr2
do
echo "arrary2: $i"
done
}
test_array=(1 2 3 4 5)
test_array2=(7 8 9 10 11)
display_two_array "${test_array[*]}" "${test_array2[*]}"
It should be noticed that the ${test_array[*]} and ${test_array2[*]} should be surrounded by "", otherwise you'll fail.
The answer below shows you how to pass bash regular "indexed" arrays as parameters to a function essentially by serializing and deserializing them.
To see this manual serializing/deserializing for bash associative arrays (hash tables) instead of for regular indexed arrays, see my answer here.
For a better way (which requires bash version 4.3 or later, I think), which passes the arrays by reference, see the link just above and my other answer here.
Passing arrays by reference is much easier and more-concise, so that's what I now recommend. That being said, the manual serializing/deserializing techniques I show below are also extremely informative and useful.
Quick summary:
See the 3 separate function definitions below. I go over how to pass:
one bash array to a function
two or more bash arrays to a function, and
two or more bash arrays plus additional arguments (before or after the arrays) to a function.
12 years later and I still don't see any answers I really like here and which I would consider to be thorough enough, simple enough, and "canonical" enough for me to just use--answers which I can come back to again and again and copy and paste and expand when needed. So, here is my answer which I do consider to be all of these things.
How to pass bash arrays as parameters to bash functions
You might also call this "variadic argument parsing in bash functions or scripts", especially since the number of elements in each array passed in to the examples below can dynamically vary, and in bash the elements of an array essentially get passed to the function as separate input parameters even when the array is passed in via a single array expansion argument like this "${array1[#]}".
For all example code below, assume you have these two bash arrays for testing:
array1=()
array1+=("one")
array1+=("two")
array1+=("three")
array2=("four" "five" "six" "seven" "eight")
The code above and below is available in my bash/array_pass_as_bash_parameter.sh file in my eRCaGuy_hello_world repo on GitHub.
Example 1: how to pass one bash array to a function
To pass an array to a bash function, you have to pass all of its elements separately. Given bash array array1, the syntax to obtain all elements of this array is "${array1[#]}". Since all incoming parameters to a bash function or executable file get wrapped up in the magic bash input parameter array called #, you can read all members of the input array with the "$#" syntax, as shown below.
Function definition:
# Print all elements of a bash array.
# General form:
# print_one_array array1
# Example usage:
# print_one_array "${array1[#]}"
print_one_array() {
for element in "$#"; do
printf " %s\n" "$element"
done
}
Example usage:
echo "Printing array1"
# This syntax passes all members of array1 as separate input arguments to
# the function
print_one_array "${array1[#]}"
Example Output:
Printing array1
one
two
three
Example 2: how to pass two or more bash arrays to a function...
(and how to recapture the input arrays as separate bash arrays again)
Here, we need to differentiate which incoming parameters belong to which array. To do this, we need to know the size of each array, meaning the number of elements in each array. This is very similar to passing arrays in C, where we also generally must know the array length passed to any C function. Given bash array array1, the number of elements in it can be obtained with "${#array1[#]}" (notice the usage of the # symbol). In order to know where in the input arguments the array_len length parameter is, we must always pass the array length parameter for each array before passing the individual array elements, as shown below.
In order to parse the arrays, I use array slicing on the input argument array, #.
Here is a reminder on how bash array slicing syntax works (from my answer here). In the slicing syntax :start:length, the 1st number is the zero-based index to start slicing from, and the 2nd number is the number of elements to grab:
# array slicing basic format 1: grab a certain length starting at a certain
# index
echo "${#:2:5}"
# │ │
# │ └────> slice length
# └──────> slice starting index (zero-based)
# array slicing basic format 2: grab all remaining array elements starting at a
# certain index through to the end
echo "${#:2}"
# │
# │
# └──────> slice starting index (zero-based)
Also, in order to force the sliced parameters from the input array to become a new array, I surround them in parenthesis (), like this, for example ("${#:$i:$array1_len}"). Those parenthesis on the outside are important, again, because that's how we make an array in bash.
This example below only accepts two bash arrays, but following the given patterns it can be easily adapted to accept any number of bash arrays as arguments.
Function definition:
# Print all elements of two bash arrays.
# General form (notice length MUST come before the array in order
# to be able to parse the args!):
# print_two_arrays array1_len array1 array2_len array2
# Example usage:
# print_two_arrays "${#array1[#]}" "${array1[#]}" \
# "${#array2[#]}" "${array2[#]}"
print_two_arrays() {
# For debugging: print all input args
echo "All args to 'print_two_arrays':"
print_one_array "$#"
i=1
# Read array1_len into a variable
array1_len="${#:$i:1}"
((i++))
# Read array1 into a new array
array1=("${#:$i:$array1_len}")
((i += $array1_len))
# Read array2_len into a variable
array2_len="${#:$i:1}"
((i++))
# Read array2 into a new array
array2=("${#:$i:$array2_len}")
((i += $array2_len))
# Print the two arrays
echo "array1:"
print_one_array "${array1[#]}"
echo "array2:"
print_one_array "${array2[#]}"
}
Example usage:
echo "Printing array1 and array2"
print_two_arrays "${#array1[#]}" "${array1[#]}" "${#array2[#]}" "${array2[#]}"
Example Output:
Printing array1 and array2
All args to 'print_two_arrays':
3
one
two
three
5
four
five
six
seven
eight
array1:
one
two
three
array2:
four
five
six
seven
eight
Example 3: pass two bash arrays plus some extra args after that to a function
This is a tiny expansion of the example above. It also uses bash array slicing, just like the example above. Instead of stopping after parsing two full input arrays, however, we continue and parse a couple more arguments at the end. This pattern can be continued indefinitely for any number of bash arrays and any number of additional arguments, accommodating any input argument order, so long as the length of each bash array comes just before the elements of that array.
Function definition:
# Print all elements of two bash arrays, plus two extra args at the end.
# General form (notice length MUST come before the array in order
# to be able to parse the args!):
# print_two_arrays_plus_extra_args array1_len array1 array2_len array2 \
# extra_arg1 extra_arg2
# Example usage:
# print_two_arrays_plus_extra_args "${#array1[#]}" "${array1[#]}" \
# "${#array2[#]}" "${array2[#]}" "hello" "world"
print_two_arrays_plus_extra_args() {
i=1
# Read array1_len into a variable
array1_len="${#:$i:1}"
((i++))
# Read array1 into a new array
array1=("${#:$i:$array1_len}")
((i += $array1_len))
# Read array2_len into a variable
array2_len="${#:$i:1}"
((i++))
# Read array2 into a new array
array2=("${#:$i:$array2_len}")
((i += $array2_len))
# You can now read the extra arguments all at once and gather them into a
# new array like this:
extra_args_array=("${#:$i}")
# OR you can read the extra arguments individually into their own variables
# one-by-one like this
extra_arg1="${#:$i:1}"
((i++))
extra_arg2="${#:$i:1}"
((i++))
# Print the output
echo "array1:"
print_one_array "${array1[#]}"
echo "array2:"
print_one_array "${array2[#]}"
echo "extra_arg1 = $extra_arg1"
echo "extra_arg2 = $extra_arg2"
echo "extra_args_array:"
print_one_array "${extra_args_array[#]}"
}
Example usage:
echo "Printing array1 and array2 plus some extra args"
print_two_arrays_plus_extra_args "${#array1[#]}" "${array1[#]}" \
"${#array2[#]}" "${array2[#]}" "hello" "world"
Example Output:
Printing array1 and array2 plus some extra args
array1:
one
two
three
array2:
four
five
six
seven
eight
extra_arg1 = hello
extra_arg2 = world
extra_args_array:
hello
world
References:
I referenced a lot of my own sample code from my eRCaGuy_hello_world repo here:
array_practice.sh
array_slicing_demo.sh
[my answer on bash array slicing] Unix & Linux: Bash: slice of positional parameters
An answer to my question on "How can I create and use a backup copy of all input args ("$#") in bash?" - very useful for general array manipulation of the input argument array
An answer to "How to pass array as an argument to a function in Bash", which confirmed to me this really important concept that:
You cannot pass an array, you can only pass its elements (i.e. the expanded array).
See also:
[another answer of mine on this topic] How to pass array as an argument to a function in Bash
Requirement: Function to find a string in an array.
This is a slight simplification of DevSolar's solution in that it uses the arguments passed rather than copying them.
myarray=('foobar' 'foxbat')
function isInArray() {
local item=$1
shift
for one in $#; do
if [ $one = $item ]; then
return 0 # found
fi
done
return 1 # not found
}
var='foobar'
if isInArray $var ${myarray[#]}; then
echo "$var found in array"
else
echo "$var not found in array"
fi
You can also create a json file with an array, and then parse that json file with jq
For example:
my-array.json:
{
"array": ["item1","item2"]
}
script.sh:
ARRAY=$(jq -r '."array"' $1 | tr -d '[],"')
And then call the script like:
script.sh ./path-to-json/my-array.json
You can find more ideas in this similar question: How to pass array as an argument to a function in Bash

Resources