I am passing multiple argument to a shell script. I that script I want to create an array from 2nd argument till the last argument. This I am able to do with below -
arg=$1
shift
while [[ $1 != '' ]]
do
emailID[${#emailID[@]}]=$1
shift
done
This emailID array I want to pass to second script as second argument and I want to retrieve the array in this second script. Is there a way to do it in ksh/bash?
Just use
${@:2}
. It is explained here perfectly.And to pass it into another script:
Of course it will not pass it as array (because you cannot pass an array), but it will pass all elements of that array as individual parameters.
Okay, lets try it this way. Here is your first script:
And here is your another script:
You can use:
The second assignment makes an array out of the remaining arguments. You might get away without the quotes around
$1
these days; it was not always thus and I use double quotes for consistency.You can then pass the array to the second script:
However, it is up to the second script to decide whether to treat the arguments as an array. There is no difference between that and passing a number of separate arguments. You can't distinguish the array from arguments before or after the array.