LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-02-2011, 04:42 AM   #1
etika
Member
 
Registered: Dec 2010
Posts: 38

Rep: Reputation: 0
passing an array as command line arguments?


how to pass an array as a command line argument in a shell script?
 
Old 04-02-2011, 04:52 AM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
http://tldp.org/LDP/abs/html/
 
Old 04-02-2011, 05:03 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 04-03-2011, 12:26 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
I think the OP wants to pass an existing array as arguments to a command, not get the arguments to his script.
 
Old 04-03-2011, 01:14 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 04-03-2011, 01:40 PM   #6
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
Quote:
Originally Posted by MTK358 View Post
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

Last edited by smoker; 04-03-2011 at 01:43 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing command line arguments jason_m Programming 4 08-26-2010 11:36 PM
Passing command-line arguments to qglviewer application MALDATA Programming 1 07-15-2009 10:27 AM
Passing command line arguments through getopts and long options neville310 Programming 3 04-16-2007 07:38 AM
Passing a text file to the command line as arguments wimnat Linux - General 2 12-05-2005 09:09 AM
passing a list of arguments to a command hdagelic Linux - General 2 05-09-2005 10:30 AM


All times are GMT -5. The time now is 12:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration