LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 05-22-2008, 05:51 PM   #1
williamhomanchun
LQ Newbie
 
Registered: Apr 2008
Posts: 17

Rep: Reputation: 0
How to use array in sh shell


Hi all,

I am trying to use array in sh shell, I try the following in sh shell, but it is not working.

Is that my syntax is wrong, or my sh shell version too old, or sh shell do not support array command, only bash shell support it ?

$ myarray=(a b c)
syntax error: `myarray=' unexpected
$ set +A myarray 'a' 'b' 'c'
$ echo ${myarray[0]}
bad substitution
$ echo ${#myarray}
bad substitution
$

Thanks for any help

William
 
Old 05-22-2008, 06:14 PM   #2
cjcox
Member
 
Registered: Jun 2004
Posts: 305

Rep: Reputation: 42
myarray[0]=a
myarray[1]=b
myarray[2]=c

echo "Index 1 has: ${myarray[1]}"

Arrays won't work in pure bourne shell.
 
Old 05-22-2008, 08:16 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
For safety's sake, always specify which shell to use at the top of your script thus:

#!/bin/bash
 
0 members found this post helpful.
Old 05-22-2008, 10:41 PM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
or you can use eval in replace for arrays if you want to use the old sh shell:

Code:
a1=one
a2=two
a3=three
for ((i=1; i<=3; i++)); do
  eval echo "value of a${i} = \${a${i}}"
  # or eval echo "value of a$i = \$a$i"
done
 
Old 04-04-2012, 07:36 AM   #5
expert_vision
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
or you can use eval in replace for arrays if you want to use the old sh shell:

Code:
a1=one
a2=two
a3=three
for ((i=1; i<=3; i++)); do
  eval echo "value of a${i} = \${a${i}}"
  # or eval echo "value of a$i = \$a$i"
done
Yes, but how do you build the array in a for?

something like:
Code:
/bin/sh
for i in `seq 1 5`; do
array$i=$i
done
won't work.
 
Old 04-05-2012, 02:08 AM   #6
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,631

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
use eval:
eval array$i=$i
 
Old 04-05-2012, 02:47 AM   #7
expert_vision
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Rep: Reputation: 0
Totally missed that one, silly me, thanks.
 
Old 04-05-2012, 10:29 AM   #8
alinas
Member
 
Registered: Apr 2002
Location: UK, Sywell, EGBK
Distribution: RHEL, SuSE, CentOS, Debian, Ubuntu
Posts: 60

Rep: Reputation: 20
Consider KSH ?

Korn shell also has arrays. Create with:
set +A myarray value1 value2 value3 ...
 
Old 04-05-2012, 10:45 AM   #9
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
array=(a b c)
is a bashism.
Works only in a modern bash (from 3.x on , I guess)
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script split array robertngo Programming 13 06-19-2011 11:01 PM
Bash Shell Scripting - using ls into array aliasofmike Programming 5 11-05-2007 03:00 PM
How to extract data and for loop it into an array? (shell) WeiSomething Programming 6 11-17-2006 03:27 AM
Shell scripting - Adding many numbers in an array Stingreen Linux - General 2 10-25-2004 05:25 PM
shell script array problem rche3252 Programming 1 10-08-2003 11:43 PM


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