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



Reply
 
Search this Thread
Old 03-06-2012, 10:40 AM   #1
ebsbel
Member
 
Registered: Jun 2005
Posts: 64

Rep: Reputation: 15
Bash: Sequence of numbers into an array [SOLVED]


Hi all,

I was tearing my beard trying to get this to work.
I simply wanted to store a sequence of numbers into an array in bash.

I first tried this:
Code:
~$ num=$(seq 2 5)
~$ echo ${num[1]}

~$ echo ${num[0]}
2 3 4 5
If you create num by writing all the numbers down:
Code:
~$ num=(2 3 4 5)
~$ echo ${num[1]}
3
You get 3 as the second element, which is what I want. Now the problem is that if you have a sequence of 1000 elements, you don't want to write them all in your code.
I could not understand what I did wrong until I realized that num was simply stored as text in the first case. I then split the text with the following command:
Code:
~$ num=(`seq 2 5 | tr " " "\n"`)
~$ echo ${num[1]}
3
I figured that more people might want to do this, so why not post it...
Maybe someone has a better solution too.
 
Old 03-06-2012, 11:54 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Well you nearly had it the first time:
Code:
num=($(seq 2 5))
You simply forgot to tell it to be an array and not a string
 
1 members found this post helpful.
Old 03-06-2012, 02:25 PM   #3
ebsbel
Member
 
Registered: Jun 2005
Posts: 64

Original Poster
Rep: Reputation: 15
Thanks Grail!

That was really simple. To think I spent more than an hour searching for a solution for that to work. At least you learn a lot in the process.
 
  


Reply

Tags
array, bash, sequence


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
[SOLVED] Bash script: comparison with sequence of numbers? dbrazeau Programming 11 03-22-2011 01:22 PM
Bash + Reading values(numbers) from a file and storing them into an array SuchANewb Linux - Newbie 5 11-04-2010 09:11 AM
next sequence of numbers ????? fachamix General 11 11-30-2009 07:52 AM
[SOLVED] regex with numbers in sequence schneidz Programming 6 04-28-2009 07:52 PM
sequence of numbers, how to extract which numbers are missing jonlake Programming 13 06-26-2006 04:28 AM


All times are GMT -5. The time now is 11:09 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration