Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to implement array in Bourne shell (/bin/sh). I found that way to initialize array elements is:

arr=(1 2 3)

But it is encountering an error:

syntax error at line 8: `arr=' unexpected

Now the post where I found this syntax says it is for bash shell, but I could not find any separate syntax for Bourne shell. Does the syntax stands the same for /bin/sh as well?

share|improve this question
    
check this question stackoverflow.com/questions/9481702/… on stack overflow –  Nischay Jun 17 at 9:41
1  
Thnx @Nischay...After reading the link you provided, I refined my query string in google and got the link -docstore.mik.ua/orelly/unix/upt/ch45_34.htm –  NoobGeek Jun 17 at 10:07

2 Answers 2

up vote 13 down vote accepted

/bin/sh is hardly ever a Bourne shell on any systems nowadays (even Solaris which was one of the last major vendors to include it have now switched to a POSIX sh for their /bin/sh in Solaris 11). /bin/sh was the Thomson shell in the early 70s. The Bourne shell replaced it in Unix V7 in 1979.

/bin/sh has been the Bourne shell for many years thereafter (or the Almquist shell, a free reimplementation on BSDs).

Nowadays, /bin/sh is more commonly an interpreter or another for the POSIX sh language which is itself a subset of the language of ksh88 (and a superset of the Bourne shell language with some incompatibilities).

The Bourne or POSIX sh don't have arrays. Or rather they have only one array: the positional parameters ($1, $2, $@, so one array per function as well).

ksh88 did have arrays which you set with set -A, but that didn't get specified in the POSIX sh as the syntax is awkward and not very usable.

Other shells with array/lists variables include: csh/tcsh, rc, es, bash (which mostly copied the ksh syntax the ksh93 way), zsh, fish each with a different syntax (rc the shell of the once to-be sucessor of Unix, fish and zsh being the most consistent ones)...

In standard sh (also works in modern versions of the Bourne shell):

set '1st element' 2 3 # setting the array

set -- "$@" more # adding elements to the array

shift 2 # popping elements from the array

printf '<%s>\n' "$@" # passing all the elements of the $@ array 
                     # as arguments to a command

for i do # looping over the  elements of the $@ array ($1, $2...)
  printf 'Looping over "%s"\n' "$i"
done

printf '%s\n' "$1" # accessing individual element of the array.
share|improve this answer
    
Thanks a ton...your detailed explanation was very helpful. –  NoobGeek Jun 17 at 10:21

There are no arrays in plain Bourne shell. You can use the following way to create an array and traverse it:

#!/bin/sh
# ARRAY.sh: example usage of arrays in Bourne Shell

array_traverse()
{
    for i in $(seq 1 $2)
    do
    current_value=$1$i
    echo $(eval echo \$$current_value)
    done
    return 1
}

ARRAY_1=one
ARRAY_2=two
ARRAY_3=333
array_traverse ARRAY_ 3

No matter what way to use arrays in sh would you pick it will always be cumbersome. Consider using a different language such as Python or Perl if you can unless you are stuck with a very limited platform or want to learn something.

share|improve this answer
    
Thanks for the response...!! Actually I am indeed trying to learn things in shell script...otherwise implementing array in Python is really a piece of cake. This was a big lesson that there exists some scripting language which does not support array :) One thing, the code you posted is giving a error - "syntax error at line 6: `$' unexpected" ... I am a little busy now, I would get it solved...plz dont bother. –  NoobGeek Jun 17 at 10:51
    
@NoobGeek, the Bourne shell doesn't have the $(...) syntax. So you must indeed have the Bourne shell. Are you on Solaris 10 or before? Chances are you won't have a seq either. On Solaris 10 and earlier, you want /usr/xpg4/bin/sh to have a standard sh instead of a Bourne shell. Using seq that way is not very good either. –  Stéphane Chazelas Jun 17 at 11:42
    
POSIX states that $ and ` are equivalent in command substitution: link. And why is using of seq that way not good? –  Arkadiusz Drabczyk Jun 17 at 12:17
    
@Stéphane Chazelas: Actually I am using '$' in some other script and that is working properly (var3=expr $var1 + $var2). –  NoobGeek Jun 17 at 13:42
1  
Yes in POSIX shells, you should prefer $(...) over `, but the OP's /bin/sh is probably a Bourne shell, not a POSIX shell. Beside seq not being a standard command, doing $(seq 100) means storing the whole output in memory, and that means it depends on the current value of $IFS containing newline and not containing digits. Best to use i=1; while [ "$i" -le 100 ]; ...; i=$(($i + 1)); done (though that wouldn't work in the Bourne shell either). –  Stéphane Chazelas Jun 17 at 13:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.