LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 09-09-2009, 08:41 AM   #1
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,850
Blog Entries: 6
Thanked: 226
Bash: all variables are arrays. Why?


[Log in to get rid of this advertisement]
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
linux catkin is offline  
Tag This Post , ,
Reply With Quote
Old 09-09-2009, 08:46 AM   #2
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 248
Thanked: 6
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.
windows_xp_2003 RaptorX is offline     Reply With Quote
Thanked by:
Old 09-09-2009, 09:04 AM   #3
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,850
Blog Entries: 6
Thanked: 226

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
linux catkin is offline     Reply With Quote
Old 09-09-2009, 09:17 AM   #4
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 248
Thanked: 6
I didnt see it was you!

Im talking like a teacher to my teacher.
windows_xp_2003 RaptorX is offline     Reply With Quote
Old 09-09-2009, 09:37 AM   #5
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,850
Blog Entries: 6
Thanked: 226

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.
linux catkin is offline     Reply With Quote
Old 09-09-2009, 09:52 AM   #6
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 248
Thanked: 6
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 09:55 AM..
windows_xp_2003 RaptorX is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
problem with arrays in bash igor.R Linux - Newbie 8 04-18-2009 12:05 PM
Bash Arrays Simon256 Programming 2 02-17-2009 02:39 PM
Arrays overwriting variables in c george_mercury Programming 4 12-31-2004 07:20 AM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 10:57 AM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 01:58 PM


All times are GMT -5. The time now is 08:13 AM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration