Question about argument parsing in scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about argument parsing in scripts
# 1  
Old 09-19-2010
Question about argument parsing in scripts

Hello all, I am relatively new to linux and bash scripting. I have what seems to be a simple question but I'm having trouble finding the answer.

The question is what is the difference between the variables $@ and $*. I've seen them both used in the same context, and I've tried a number of different scripts to determine the difference but they both seem to do the exact same thing.

Any help would be greatly appreciated. Thanks in advance.
# 2  
Old 09-19-2010
"$@" when used in double quotes, preserves the quoting from the command line, $* does not. A simple script that you can write to show this is:

Code:
#/usr/bin/env ksh

for x in "$@"
do
        echo $x
done

echo "==========================="
for x in "$*"
do
        echo $x
done

Executing it with differently quoted tokens on the command line you get this output:

Code:
>: t25 now "is the time" for all "good men"
now
is the time
for
all
good men
===========================
now is the time for all good men

t25 is the script name that I used, >: is my prompt.

If you use $* without the quotes, you get each word without regard to the quoting on the command line:

Code:
now
is
the
time
for
all
good
men

This User Gave Thanks to agama For This Post:
# 3  
Old 09-19-2010
Awesome, thank you for the response. I noticed something else with that output as well. You used the same loop to print out the arguments yet $@ printed all the arguments on separate lines and $* printed them all together. Just curious as to what the reason is for that. It must have something to do with the for loop since if you do the same loop without quotes around the variables, they both print the arguments on separate lines.

Edit: After re reading your post it seems that we're talking about the same thing. Would you be able to explain the reason that happens?

Thanks again.
# 4  
Old 09-19-2010
The expansion of the contents of "$@", "$*", and $* are different, which results in the differing results. Assuming that a "b c" d e was entered on the command line, then "$@" results in this expansion on the for loop (quoting from the command line is preserved):

Code:
 for  x in  a "b c" d e

while "$*" results in:

Code:
 for x in "a b c d e"

Here the command line arguments are converted to a single token and thus the loop executes only once with "a b c d" assigned to x.

For the last example, $* (no quotes) evaluates to just a b c d on the loop statement (quoting from the command line is not preserved) and thus the loop is executed one time for each and thus each is presented on a separate line.

Hope this makes it more clear.
# 5  
Old 09-19-2010
Yeah, that makes sense. Thanks so much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question about getopts optional argument [args...]

There are many places where I can see the syntax description for optargs, which, usually boils down to this: getopts OPTSTRING VARNAME where: OPTSTRING tells getopts which options to expect and where to expect arguments VARNAME tells getopts which shell-variable to use for option reporting... (2 Replies)
Discussion started by: sharkura
2 Replies

2. Shell Programming and Scripting

parsing argument in perl

in bash: LIST=`cat $1` for i in $LIST do ... done how will i do this in perl ? $1 is my first arguement. I'm a newbie in perl and will appreciate much your help guys ... (4 Replies)
Discussion started by: linuxgeek
4 Replies

3. Shell Programming and Scripting

Perl Parsing Argument

i wanna passing an argument which read in a file or a set of files if the files are given in the command line, otherwise use STDIN if no file argument. i got something like that, but it is not really working. so can anyone help me? which one is better to use for and how? Use perl. Thank you ... (0 Replies)
Discussion started by: mingming88
0 Replies

4. Shell Programming and Scripting

Parsing question

Hi Guys, I was wondering if you could help me out - I have a directory /home/users/datafiles/ which contain files "dat dd-mm-yy.xls" I am trying to write a script which does the following - (1) loops through all the files (2) retrieves the dd-mm-yy string and converts it into a... (12 Replies)
Discussion started by: muser
12 Replies

5. Shell Programming and Scripting

problem with spaces and argument parsing

public class HelloWorld { public static void main(String args) { System.out.println("Welcome, master"); } } and I compiled using javac HelloWorld.java ] Suppose that I execute the following command directly from the shell: java -XX:OnError="gdb - %p" HelloWorld Then it works... (8 Replies)
Discussion started by: fabulous2
8 Replies

6. UNIX for Dummies Questions & Answers

Parsing text from one line with shell scripts

Hi Gurus! I wonder if anyone can help me, I'm sure you guys can. I have a text file which contains a lot of data on the one line as follows: $ What I need to do is pull all of those id values out (eg 2549425) and write them to a list in a text file. Any help would be greatly... (3 Replies)
Discussion started by: th3g0bl1n
3 Replies

7. UNIX for Dummies Questions & Answers

Very simple argument in scripting question

I tried to do a search, but it couldnt pinpoint what my answer since using limited but broad keywords. Sorry in advance ; ; Im limited to using Bourne shell scripting only, atm I have the following code (just the heading part of it) ... ... # VARIABLE DECLARATION # ==================== ... (2 Replies)
Discussion started by: eeto
2 Replies

8. Shell Programming and Scripting

argument parsing...

Hi all, Iam a beginer in shell scripting. i need a script that can parse the arguments and store them in variables. ex: ./myScript -v v1 -h v2 -c v3...... can someone suggest me...? tnx in adv. (1 Reply)
Discussion started by: midhun_u
1 Replies

9. UNIX for Dummies Questions & Answers

command line argument parsing

how to parse the command line argument to look for '@' sign and the following with '.'. In my shell script one of the argument passed is email address. I want to parse this email address to look for correct format. rmjoe123@hotmail.com has '@' sign and followed by a '.' to be more... (1 Reply)
Discussion started by: rmjoe
1 Replies

10. Shell Programming and Scripting

shell script argument parsing

how to parse the command line argument to look for '@' sign and the following with '.'. In my shell script one of the argument passed is email address. I want to parse this email address to look for correct format. rmjoe123@hotmail.com has '@' sign and followed by a '.' to be more... (1 Reply)
Discussion started by: rmjoe
1 Replies
Login or Register to Ask a Question