LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-09-2009, 08:41 AM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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
 
Old 09-09-2009, 08:46 AM   #2
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
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.
 
Old 09-09-2009, 09:04 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Original Poster
Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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
 
Old 09-09-2009, 09:17 AM   #4
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
I didnt see it was you!

Im talking like a teacher to my teacher.
 
Old 09-09-2009, 09:37 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Original Poster
Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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.
 
Old 09-09-2009, 09:52 AM   #6
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
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.
 
  


Reply

Tags
array, bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03: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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration