Shell string array

Hi all,

I want to create an array variable in shell, for example:

#!/bin/sh
name[0]="foo"
name[1]="bar"
name[2]="baz"

But above code didn't work, I also tried with:

name=(foo bar baz)

and

set -A name foo bar baz

but none of these worked.

Another question is how to know the shell version on my Ubuntu system?
I tried

/bin/sh --version

and

sh --version

, but both failed.

Thanks for your help.
Roy987

/bin/sh on ubuntu is always bash. Unless you opted to use another POSIX compliant shell on install.

Does

ls -l /bin/sh /bin/bash /usr/bin/bash
echo $SHELL

provide any enlightenment?

1 Like

The following is the output from terminal with command

ls -l /bin/sh

output:

lrwxrwxrwx 1 root root 4 2011-05-07 19:49 /bin/sh -> dash

I didn't change anything, so /bin/sh should default point to dash, maybe there isn't array in dash.
Thanks.

/bin/sh on Ubuntu is dash, it is fast, but no-thrills and strictly POSIX compliant shell and therefore it does not know arrays. Arrays should not be used in combination with /bin/sh (on any system), if it does work it is pure happenstance.

Use /bin/bash or /bin/ksh (which is /bin/ksh93) instead..

What does that means ?

Beware that on Linux, ksh is more commonly pdksh, which doesn't support all arrays features ksh93 and bash provide.

LOL, I meant to say arrays.. Corrected in post.
On Ubuntu ksh is the real ksh, and I think on most modern Linux distributions the default ksh has become ksh93.

1 Like

Thanks.
Quite beyond my question. Really learned a lot.