3

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?

1
  • From the shell tag wiki, it seems like it shouldn't be used with the bash tag. Did you have a reason to put them together? Commented Oct 17, 2022 at 3:05

2 Answers 2

4

Just use ${@:2}. It is explained here perfectly.

And to pass it into another script:

./anotherScript "${@:2}"

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:

#!/bin/bash
echo "Here is my awesome first argument: $1"
var=$1 # I can assign it into a variable if I want to!
echo 'Okay, and here are my other parameters!'
for curArg in "${@:2}"; do
    echo "Some arg: $curArg"
done
myArr=("${@:2}") # I can assign it into a variable as well!

echo 'Now lets pass them into another script!'
./anotherScript 'Wheeee' "${@:2}"

And here is your another script:

#!/bin/bash
echo 'Alright! Here we can retrieve some arguments!'
echo "Here we go: $@" # that is including 'Wheeee' from the previous script. But we can use the same trick from above if we want to.
Sign up to request clarification or add additional context in comments.

6 Comments

Hi Aleks, How to retrive this array in second script?
@user3780855 if it's the only parameter the other script takes, just use arrayvar=("$@"). If the other script takes other arguments, you'll have to figure out a way to tell it where the array starts and ends. The parameters are really just a series of words, there's no standard way to say "parameters 3 through 7 are an array". If you want more help on this, you'll have to tell us more about the other script and its arguments.
Hi Gordon, As i said I want to do something like this - b="${@:2}"./anotherScript $a $b And in another script - If i have to retrive the array then how do i do it? is this the correct way? anotherScript - var=$1; array="${@:2}"
@GordonDavisson Well, if you want to use parameters 3 through 7 as an array you can just use "${@:3:5}".
@user3780855 It is incorrect. You have to quote variables like so ./anotherScript "$a" "${emailID[@]}", also @ makes more sense here. Also emailID="${@:2}" does not give you an array, it will be a string. If you want it to be an array then do emailID=("${@:2}")
|
2

You can use:

arg="$1"
shift
emailID=( "$@" )

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:

second_script "${emailID[@]}"

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.

10 Comments

$@ is an array already, so why would you want to create another array out of it? Well, if you want to modify its elements, then OK.
In which shell do you think it is an array?
The question is marked as bash so I mean bash. It depends on the definition of 'array', but basically it works exactly like that.
I'm just saying that you can treat $@ as an array in most cases, unless you want to change its values. Technically you're right, it is not exactly an array, but it is enough array-ish that we can skip that part. You see, all other parameter expansions will work on it like if it was an array, try echo "${@^}" for example.
@mklement0: I've not checked the Bash change logs either; assignment without quotes may always have worked in Bash. Indeed, I just tried the Heirloom (Bourne) Shell, and it seems that the assignment works there without quotes, somewhat to my surprise. I can't go back and resurrect the machines from the early 80s where I learned shell to determine whether I'm misremembering after all these years. The 1978 paper on the Bourne shell doesn't illustrate what happens with x="1 2"; y=$x.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.