LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 04-09-2011, 11:50 AM   #1
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Rep: Reputation: 0
Bash $@, $*, "$*", "$@"


Dear People,
could someone please give a brief description of what the title mentioned expressions do? I googled it but all i find is weired useless quote pages (the internet is getting worse and worse it seems).
Especially I would be interested in understanding what the difference between the unquoted and quoted versions of the same expression is, ie. $@ vs "$@".

Thanks for any hints or posts.
 
Old 04-09-2011, 12:41 PM   #2
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
The answer is here:
http://www.grymoire.com/Unix/Sh.html#uh-42
 
1 members found this post helpful.
Old 04-09-2011, 12:54 PM   #3
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
The difference is word splitting. The most common case where this matters is if you have file names with spaces in them. Here is my unsophisticated explanation.
  • $* arguments are separated by spaces
  • "$*" means space does not separate arguments
  • $@ arguments are separated by spaces
  • "$@" arguments are separated by unquoted spaces

This is all fine and good until you bring IFS into the mix. I'll leave it to others to go into that except to say that you shouldn't change IFS from its default value unless you have a very specific need to do so and you remember to change it back immediately.

So here's a short, simple Bash script to demonstrate what's happening.

Code:
#! /bin/bash

echo 'parsing with $*'
i=0
for a in $*
do
   i=$((i+1))
   echo "arg $i = \`$a'"
done
echo "$i args passed"

echo

echo 'parsing with "$*"'
i=0
for a in "$*"
do
   i=$((i+1))
   echo "arg $i = \`$a'"
done
echo "$i args passed"

echo

echo 'parsing with $@'
i=0
for a in $@
do
   i=$((i+1))
   echo "arg $i = \`$a'"
done
echo "$i args passed"

echo

echo 'parsing with "$@"'
i=0
for a in "$@"
do
   i=$((i+1))
   echo "arg $i = \`$a'"
done
echo "$i args passed"
Here's a sample run.

Code:
foo$ ./parseargs one two
parsing with $*
arg 1 = `one'
arg 2 = `two'
2 args passed

parsing with "$*"
arg 1 = `one two'
1 args passed

parsing with $@
arg 1 = `one'
arg 2 = `two'
2 args passed

parsing with "$@"
arg 1 = `one'
arg 2 = `two'
2 args passed
foo$
What happens when we quote the space between arguments?

Code:
foo$ ./parseargs one\ two
parsing with $*
arg 1 = `one'
arg 2 = `two'
2 args passed

parsing with "$*"
arg 1 = `one two'
1 args passed

parsing with $@
arg 1 = `one'
arg 2 = `two'
2 args passed

parsing with "$@"
arg 1 = `one two'
1 args passed
foo$
What happens when we pass no arguments at all?

Code:
foo$ ./parseargs
parsing with $*
0 args passed

parsing with "$*"
arg 1 = `'
1 args passed

parsing with $@
0 args passed

parsing with "$@"
0 args passed
foo$
Most times you will probably want "$@", but there may be cases where the others are more desirable.

I invite informative expansions and corrections on all the above.

HTH
 
2 members found this post helpful.
Old 04-09-2011, 01:44 PM   #4
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Original Poster
Rep: Reputation: 0
@Telengard: Thank you a lot for the comprehensive explanation. This makes it a lot easier for me to understand it.
 
Old 04-09-2011, 04:16 PM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
If you feel your question has been adequately answered then please consider using th thread tools to mark this thread solved.

Your blog is nifty. I like that you use it to share the solutions you find.
 
Old 04-10-2011, 04:20 PM   #6
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Telengard View Post
If you feel your question has been adequately answered then please consider using th thread tools to mark this thread solved.

Your blog is nifty. I like that you use it to share the solutions you find.
Even more thanks for that. Yeah, its kind of a convenient way to keep track of solutions of particular use to the kind of things i need to be able to do.
 
  


Reply

Tags
argument, parameter, shell, word splitting


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 11:35 AM
BASH:find out if volume "foo"/folder "goo" can be written to SilversleevesX Programming 2 08-28-2010 11:03 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 09:31 AM
bash-2.05b# Xlib: extension "XFree86-DRI" missing on display ":0.0". citrus Linux - General 8 02-22-2004 11:43 AM


All times are GMT -5. The time now is 03:09 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration