LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-28-2010, 07:27 PM   #1
robotsari
LQ Newbie
 
Registered: Jun 2010
Location: boston & sf bay, depending on the day
Distribution: ubuntu, rhel, debian
Posts: 20

Rep: Reputation: 1
Bash scripting - what is "$#" ?


Hi,

I'm encountering this line in a script:

Code:
while [ $# -gt 0 ]; do
When I echo this to stdio on my system, $# is always 0; but it's a boottime script and when I echo & save the output, it is always 4. I'm new to bash scripting and this isn't my script; I'm just trying to debug why some stuff isn't working as I expect. I doubt this is the cause but I'm still curious what variable this might refer to.

Also if this is something I could Google, I would appreciate some good keywords to try. Being new to bash I didn't know what to look for, and symbols like $# together don't work.

Cheers!
 
Old 06-28-2010, 07:49 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
It means the number of parameters on the command line when the script is called.

Check the link in my signature for the Bash reference.

jlinkels
 
Old 06-28-2010, 11:16 PM   #3
robotsari
LQ Newbie
 
Registered: Jun 2010
Location: boston & sf bay, depending on the day
Distribution: ubuntu, rhel, debian
Posts: 20

Original Poster
Rep: Reputation: 1
Great, thanks a ton
Excellent reference, I'll start digesting it tonight!
 
Old 06-29-2010, 12:10 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Please mark as SOLVED once you have a solution.
 
Old 06-29-2010, 05:08 AM   #5
ryan858
Member
 
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43

Rep: Reputation: 17
Yeah, that line basically says "while any arguments were given from command-line, do..."

And just by the way, $1, $2, $3, etc is the specific argument on the command-line, e.g arg1, arg2, arg3, and so on. And $@ is the entire string of arguments, e.g. "arg1 arg2 arg3 arg4" and so on.

Last edited by ryan858; 06-29-2010 at 05:10 AM.
 
  


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 Off
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 7 12-19-2015 01:03 AM
bash scripting sending "cat: write error: Broken pipe," rojoblandino Linux - Server 8 04-23-2010 10:23 AM
Bash scripting issue with "source" or . (dot) operator (Cygwin & Ubuntu) brian.hussey Programming 3 01-22-2010 01:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Bash scripting "skip to"? WingnutOne Linux - Newbie 4 09-25-2007 03:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:14 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