LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-29-2008, 12:29 PM   #16
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037

Thank you very much. Actually, I finally found it for myself not long after I posted. There's a good example of it on the "special variables" page, referencing positional variables. But by the time I came back, I had two answers. You guys are fast.

I see now that it's an interesting use of indirect referencing...it outputs the value of the last positional variable, as given by "$#". Easy once you know what the parts mean.

But now why is it when I try it with the old syntax, and use 'echo \$$#', it doesn't work? It only give me the output "$3" (for example) when I try it, instead of the actual value.

Edit: Found the reason, I think. You need to eval the older notation before it will work. 'eval echo \$$#' seems to do the trick. I still don't quite understand exactly why you need to do this though.

Last edited by David the H.; 12-29-2008 at 12:41 PM.
 
Old 12-29-2008, 12:44 PM   #17
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by David the H. View Post
But now why is it when I try it with the old syntax, and use 'echo \$$#', it doesn't work? It only give me the output "$3" (for example) when I try it, instead of the actual value.
You have to use the old syntax for indirect reference in conjunction to the eval built-in. Running
Code:
echo \$$#
the shell substitutes the variable $# with the actual number of arguments and interprets the escaped $ sign as a litteral $. Using eval instead
Code:
eval echo \$$#
first the shell does the above substitution, then passes the resulting string "echo $3" to the eval bult-in, which in turn causes the substitution of $3 with its actual value.

Edit: you are faster than us, now!

Last edited by colucix; 12-29-2008 at 12:46 PM.
 
Old 12-29-2008, 12:59 PM   #18
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Thank you for the explanation. I kind of suspected that that was what was happening, but I wasn't sure. I'm still not quite clear on exactly what things like eval do sometimes.

I suppose the new ${!} notation bypasses all that and just grabs the value directly then? It's certainly easier and much more intuitive in any case.
 
Old 12-29-2008, 01:02 PM   #19
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Deleted. Accidentally duplicated my post.

Last edited by David the H.; 12-29-2008 at 01:04 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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP pass argument to shell script monzter Programming 2 08-14-2004 06:16 AM
How to count number of argument received by a script? philipina Linux - General 2 07-05-2004 02:35 AM
Script Problem - Does not give expected results Jose Muņiz Linux - General 3 07-12-2003 10:10 PM
PHP Script argument passing error... lokee Linux - Software 5 04-24-2003 09:42 AM
Is it possible to give MSDOS commands in Unix script??? gopi_20us Programming 1 02-20-2002 12:48 AM

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

All times are GMT -5. The time now is 07:13 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
Open Source Consulting | Domain Registration