LinuxQuestions.org
Review your favorite Linux distribution.
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 07-26-2006, 04:56 AM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
A strange sign



#!/bin/bash

array=(red green blue yellow magenta)

len=${#array[*]}

echo "The array has $len members. They are:"

i=0
while [ $i -lt $len ]; do
echo "$i: ${array[$i]}"
let i++
done


-----------------------------------------------

Usually this sign [#] is for comments. If you look at the above, you will read this ---> len=${#array[*]} .

What is the use of it here? I would like to read your comments.
 
Old 07-26-2006, 05:12 AM   #2
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
This is what the bash manual says:
Quote:
A command may not follow a comment on the same line. There is no method of terminating the comment, in order for "live code" to begin on the same line. Use a new line for the next command.
but after executing the script, i got the following:
Quote:
The array has 5 members. They are:
0: red
1: green
2: blue
3: yellow
4: magenta
 
Old 07-26-2006, 05:17 AM   #3
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
Of course, an escaped # in an echo statement does not begin a comment. Likewise, a # appears in certain parameter substitution constructs and in numerical constant expressions.

Quote:
echo "The # here does not begin a comment."
echo 'The # here does not begin a comment.'
echo The \# here does not begin a comment.
echo The # here begins a comment.

echo ${PATH#*:} # Parameter substitution, not a comment.
echo $(( 2#101011 )) # Base conversion, not a comment.
The standard quoting and escape characters (" ' \) escape the #.

Certain pattern matching operations also use the #.

Last edited by prozac; 07-26-2006 at 05:18 AM.
 
Old 07-26-2006, 05:49 AM   #4
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Thanks Prozac for the reply.

So it is doing some conversion here.


I can't fathom out the conversion here.
 
Old 07-26-2006, 08:28 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quoting bash's man page:
Code:
${#parameter}
       The length in characters of the value of  parameter  is  substi-
       tuted.   If  parameter  is  * or @, the value substituted is the
       number of positional parameters.  If parameter is an array  name
       subscripted  by  *  or @, the value substituted is the number of
       elements in the array.
 
Old 07-26-2006, 08:51 AM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
The ${#var} syntax is derived from Korn Shell. As Hko reports above it is for getting lengths or number of elements.
 
  


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
sign on invisible in gaim - NOT invi after sign on saravkrish Linux - Software 7 09-12-2005 10:55 PM
What is a SIGN File? giddyupman Linux - Software 1 09-28-2004 09:16 AM
Linux and $ sign blubbfish Linux - Newbie 4 03-07-2004 07:52 AM
sign in hythemr Red Hat 1 08-31-2003 07:52 PM
$,# sign eye Linux - Software 2 05-31-2003 09:00 AM

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

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

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