LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash scripting - what is "$#" ? (https://www.linuxquestions.org/questions/programming-9/bash-scripting-what-is-%24-816887/)

robotsari 06-28-2010 07:27 PM

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!

jlinkels 06-28-2010 07:49 PM

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

robotsari 06-28-2010 11:16 PM

Great, thanks a ton :)
Excellent reference, I'll start digesting it tonight!

grail 06-29-2010 12:10 AM

Please mark as SOLVED once you have a solution.

ryan858 06-29-2010 05:08 AM

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.


All times are GMT -5. The time now is 11:35 PM.