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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-09-2009, 07:41 AM
|
#1
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Bash: all variables are arrays. Why?
Hello
Is the following behaviour documented anywhere?
Code:
c@CW8:~$ scalar='string'
c@CW8:~$ echo ${#scalar[*]}
1
c@CW8:~$ echo ${scalar[0]}
string
Best
Charles
|
|
|
|
09-09-2009, 07:46 AM
|
#2
|
|
Member
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254
Rep:
|
Quote:
|
Is the following behaviour documented anywhere?
|
Yes... Everywhere...
Actually what happens is that bash variables are untyped.
That means that it will treat that variable as you are referring to it at the moment.
this link should clarify that for you.
this little quote is also an example of it:
Quote:
Bash permits array operations on variables, even if the variables are not explicitly declared as arrays.
string=abcABC123ABCabc
echo ${string[@]} # abcABC123ABCabc
echo ${string[*]} # abcABC123ABCabc
echo ${string[0]} # abcABC123ABCabc
echo ${string[1]} # No output!
# Why?
echo ${#string[@]} # 1
# One element in the array.
# The string itself.
# Thank you, Michael Zick, for pointing this out.
Once again this demonstrates that Bash variables are untyped.
|
|
|
|
|
09-09-2009, 08:04 AM
|
#3
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
Original Poster
|
Thanks RaptorX
I knew bash variables are untyped -- but not that it went as far as scalar variables being single element arrays! Truly bash is full of surprises!
Best
Charles
|
|
|
|
09-09-2009, 08:17 AM
|
#4
|
|
Member
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254
Rep:
|
I didnt see it was you!
Im talking like a teacher to my teacher. 
|
|
|
|
09-09-2009, 08:37 AM
|
#5
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
Original Poster
|
That's the great thing about co-operation, huh?
The day we stop learning is, er, ... the day we don't learn any more!
The ABSG is a great resource, especially for examples, but I always turn to the GNU Bash Reference Manual and the The Open Group's Shell Command Language Specialisation when in doubt. I couldn't find anything relevant in them so posted ... and you gave me the answer, thank you. 
|
|
|
|
09-09-2009, 08:52 AM
|
#6
|
|
Member
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254
Rep:
|
You know that I have been reading this Advanced Bash Scripting thingy... it is actually a wealth of information and I dont find it sleep provoking as the bash manual (I cant read more than 2 pages of man bash)
It is something that almost all linux users should read even if is a quick read.
[edit]
Actually my answer to your other post (the one with $[]) comes also from the ABS book.
Last edited by RaptorX; 09-09-2009 at 08:55 AM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:44 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|