myFunction()
{
> /tmp/file_$1_$2
}
ELEMENTS=("first" "second" "a third" "a fourth")
for elem in "${ELEMENTS[@]}"
do
myFunction "$elem"
done
I've tried the above code and a bunch of other variants found online:
BASH array with spaces in elements
Loop through array of arrays of string with spaces
But I could never get this to work. What I want my code to do is iterate over these elements:
firstseconda thirda fourth
pass them to myFunction but have the function treat for example a third as 2 arguments, not 1! So I would end up with 4 files:
/tmp/file_first_/tmp/file_second_/tmp/file_a_third/tmp/file_a_fourth
Whatever I try it either treats every word as a different element (6 calls to myFunction) or treats e.g. a third as one argument and creates a file /tmp/file_a third_ or something different yet.
How do I do this right in bash?