LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-06-2010, 10:24 AM   #1
stormcloud
Member
 
Registered: Apr 2009
Posts: 32

Rep: Reputation: 15
How to pass all arguments to a function


I've got a sticky little problem with a bash script. Please consider the following code:

Code:
  #! /bin/bash

  processArgs() {
    echo "Count = $#"

    while [[ $# -ne 0 ]]; do
      echo $1
      shift
    done
  }

  processArgs $*
If I call this script with

Code:
  ./script first second third
it'll print each of the argument on a new line - exactly what I would expect. However if I call it with

Code:
  ./script "Single Argument" "Second-Argument"
it splits the first argument in two using the space as a delimitor.

The problem appears to be the call to processArgs, where $* doesn't honour the quotes around the variables sent to the script.

Any ideas how to get around this?
 
Old 07-06-2010, 10:33 AM   #2
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
AFAIK, the notation "$@" (including the quotes) should produce a correctly re-quoted list of the arguments like they were initially passed. So you could try this instead of $*.
 
1 members found this post helpful.
Old 07-07-2010, 02:38 AM   #3
stormcloud
Member
 
Registered: Apr 2009
Posts: 32

Original Poster
Rep: Reputation: 15
That works :-)

I had tried plain old $@ (without quotes), which didn't work. I didn't realise that I needed the quotes.

Thanks.
 
Old 07-07-2010, 05:32 AM   #4
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
The special thing about $@ is that, when found inside double quotes, it results in EACH of the positional arguments double-quoted individually. $* in double quotes will instead double-quote the whole argument list, resulting in only one big string.

As far as I remember, there is no difference between them when not quoted (just replaced by the plain argument list, thus letting the shell split it at whitespace characters)
 
Old 07-08-2010, 03:15 AM   #5
stormcloud
Member
 
Registered: Apr 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Ah! I was wondering what the difference between them was.

Cheers
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to pass arguments to a function using a loop 10110111 Programming 2 03-01-2010 03:24 PM
How to pass arguments in a shell script? Drigo Linux - Newbie 1 11-14-2009 11:55 AM
pass arguments and return values from a function in UNIX gaynut Programming 4 07-10-2008 01:56 AM
How to pass structure as function arguments ssg14j Programming 2 08-20-2005 09:59 PM
How can we pass arguments to installpkg ? frenchi Slackware 3 04-23-2004 06:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:09 PM.

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
Open Source Consulting | Domain Registration