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 06-19-2009, 12:39 PM   #1
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Dynamic Arrays in Bash


Ok...so I've been searching google and man pages and such for an answer to this...

I mostly work in ksh...so...

What is the equivalent of:
Code:
set -A list $(ls -1)
In bash? It seems like there is no way to "dynamically" setting arrays in bash...

-C
 
Old 06-19-2009, 01:10 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
A=$(ls -1)
but I don't understand what you mean by "dynamically"?
maybe:
A=($(ls -1))

Last edited by Guttorm; 06-19-2009 at 01:13 PM.
 
Old 06-19-2009, 01:16 PM   #3
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979

Original Poster
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by Guttorm View Post
A=$(ls -1)
but I don't understand what you mean by "dynamically"?
Cool that works...

Code:
chrish@aardvark:~<10>$ A=$(ls -1 *.rpm)
chrish@aardvark:~<11>$ for i in ${A[*]}
> do
> echo $i
> done
php-5.2.9-1.src.rpm
php-eaccelerator-5.2.9_0.9.5.2-1.src.rpm
php-extras-5.2.9-1.src.rpm
Perfect!

About the "dynamic" thing...it's that I'm not familiar with the terminology.

Before I learned what you taught be above...I used to have to "manually" assign arrays like this...

Code:
rpms[1]=php-5.2.9-1.src.rpm
rpms[2]=php-eaccelerator-5.2.9_0.9.5.2-1.src.rpm
rpms[3]=php-extras-5.2.9-1.src.rpm

for rpm in ${rpms[*]}
do
  echo ${rpm}
done
But I can do this...
Code:
rpms=$(ls -1 *.rpm)

for rpm in ${rpms[*]}
do
  echo ${rpm}
done
Thanks again!

-C

Last edited by custangro; 06-19-2009 at 01:37 PM.
 
Old 06-19-2009, 01:49 PM   #4
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979

Original Poster
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by Guttorm View Post
A=$(ls -1)
but I don't understand what you mean by "dynamically"?
maybe:
A=($(ls -1))
Another question...

What is...

Code:
set -A files myfile1.txt myfile2.txt myfile3.txt
In bash?

-C
 
Old 06-19-2009, 01:59 PM   #5
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Quote:
Originally Posted by custangro View Post
Another question...

What is...

Code:
set -A files myfile1.txt myfile2.txt myfile3.txt
In bash?

-C
An indexed array? That would be

Code:
 files=(myfiles1.txt myfile2.txt myfiles3.txt)
 
Old 06-19-2009, 02:13 PM   #6
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979

Original Poster
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by Uncle_Theodore View Post
An indexed array? That would be

Code:
 files=(myfiles1.txt myfile2.txt myfiles3.txt)
Perfect!

Thanks!

-C
 
  


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
Initializing arrays in bash Raht Programming 1 03-12-2009 12:33 PM
Bash Arrays Simon256 Programming 2 02-17-2009 01:39 PM
LXer: Bash Arrays LXer Syndicated Linux News 0 06-20-2008 06:20 PM
bash arrays question introuble Programming 1 05-20-2006 03:07 AM
Dynamic arrays in c++ genderbender Programming 4 12-06-2005 04:20 AM

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

All times are GMT -5. The time now is 07:44 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