LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   passing an array as command line arguments? (https://www.linuxquestions.org/questions/linux-newbie-8/passing-an-array-as-command-line-arguments-872539/)

etika 04-02-2011 03:42 AM

passing an array as command line arguments?
 
how to pass an array as a command line argument in a shell script?

smoker 04-02-2011 03:52 AM

http://tldp.org/LDP/abs/html/

colucix 04-02-2011 04:03 AM

The list of arguments is actually an array. It is referenced by "$@" or "$*". For example, suppose you pass six arguments to your script. The number of elements (arguments) is given by "$#" or using the array notation "${#@}". Suppose you want to reference the arguments from the 3th to the 5th:
Code:

echo "${@:3:3}"
where the syntax here is "${@:start:count}". Googlin' around I found a detailed insight about the usage of positional parameters: http://wiki.bash-hackers.org/scripting/posparams. Anyway, if you explain your requirement a bit more maybe someone can offer a more specific help.

MTK358 04-03-2011 11:26 AM

I think the OP wants to pass an existing array as arguments to a command, not get the arguments to his script.

colucix 04-03-2011 12:14 PM

Quote:

Originally Posted by MTK358 (Post 4312457)
I think the OP wants to pass an existing array as arguments to a command, not get the arguments to his script.

Good catch! Let's wait for the response.

smoker 04-03-2011 12:40 PM

Quote:

Originally Posted by MTK358 (Post 4312457)
I think the OP wants to pass an existing array as arguments to a command, not get the arguments to his script.

AFAIK, that is both ends of the same stick.
I think that an array that's passed as an argument will be interpreted as an array, but there should be a single argument first.

Code:

calling_function()
{
    variable="a"
    array=( "x", "y", "z" )
    called_function "${variable}" "${array[@]}"
}

called_function()
{
    local_variable="${1}"
    shift
    local_array=("${@}")
}

That solution was from here :
http://stackoverflow.com/questions/1...meters-in-bash


All times are GMT -5. The time now is 06:23 AM.