Bash scripting -- Usage of arrays

John Baldwin jhb at
Tue Nov 29 21:29:26 GMT 2005

On Tuesday 29 November 2005 04:09 pm, Jayesh Jayan wrote:
> Hi,
> Today I was trying to script using arrays in FreeBSD 5.4 but it doesn't
> work.
> Below is a sample script which I used.
> ******************************************************
> #!/bin/bash
> array=( zero one two three four);
> echo "Elements in array0:  ${array[@]}"
> ******************************************************
> It works fine on RedHat server.
> Below is the output.
> # sh
> Elements in array0:  zero one two three four
> Below is the out put from the FreeBSD server using the same code.
> -bash-2.05b# sh
> 3: Syntax error: word unexpected (expecting ")")
> Please guide me on how to use arrays on freebsd too.

sh != bash

You can either install bash from ports, or you can write your scripts in sh 
without using bash extensions.  For example, with sh you can do things like:

array="zero one to three four"
for x in $array; do
	echo $x

However, you can't easily get the count of items.  You could maybe do 
something like:

set $array
echo "$# items"

but that's somewhat hackish.

John Baldwin <jhb at>  <><
"Power Users Use the Power to Serve"  =

More information about the freebsd-hackers mailing list