# split string into array in shell

**URL:** <https://community.unix.com/t/split-string-into-array-in-shell/310292>\
**Category:** Shell Programming and Scripting\
**Created:** [May 16, 2012, 6:28am UTC](https://community.unix.com/t/split-string-into-array-in-shell/310292 "2012-05-16T06:28:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy987](https://community.unix.com/letter_avatar/roy987/32/6_449c533f586939b6e5f246632a46421f.png) [@Roy987](https://community.unix.com/u/Roy987)\
**Post date:** [May 16, 2012, 6:28am UTC](https://community.unix.com/t/split-string-into-array-in-shell/310292/1 "2012-05-16T06:28:34Z")

</div>

Hi all,

I want to split a string into array based on given delimiter, for example:

String:

```nohighlight
 "foo|bar|baz" 

```

with delimiter "|"  
into array:  
strArr[0] to strArr[2] with values foo, bar and baz.

Thanks a lot.  
Roy987

---

<div class="post-metadata">

**Author:** ![ningy](https://community.unix.com/user_avatar/community.unix.com/ningy/32/1496_2.png) [@ningy](https://community.unix.com/u/ningy)\
**Post date:** [May 16, 2012, 7:01am UTC](https://community.unix.com/t/split-string-into-array-in-shell/310292/2 "2012-05-16T07:01:40Z")

</div>

```nohighlight
san@~ :> declare -a myarr=(`echo "foo|bar|baz" |sed 's/|/ /g'`)
san@~ :> echo ${myarr[@]}

```

foo bar baz

---

<div class="post-metadata">

**Author:** ![gary\_w](https://community.unix.com/user_avatar/community.unix.com/gary_w/32/2088_2.png) [@gary\_w](https://community.unix.com/u/gary_w)\
**Post date:** [May 16, 2012, 9:52am UTC](https://community.unix.com/t/split-string-into-array-in-shell/310292/3 "2012-05-16T09:52:34Z")

</div>

```nohighlight
$ cat x
#!/bin/ksh

IFS="|"
STRING="foo|bar|baz"

set -A array $STRING

print ${array[0]}
print ${array[1]}
print ${array[2]}

exit 0
$ x
foo
bar
baz
$

```

---

<div class="post-metadata">

**Author:** ![Roy987](https://community.unix.com/letter_avatar/roy987/32/6_449c533f586939b6e5f246632a46421f.png) [@Roy987](https://community.unix.com/u/Roy987)\
**Post date:** [May 16, 2012, 8:27pm UTC](https://community.unix.com/t/split-string-into-array-in-shell/310292/4 "2012-05-16T20:27:43Z")

</div>

> [@ningy](#):
>
> ```plaintext
> san@~ :> declare -a myarr=(`echo "foo|bar|baz" |sed 's/|/ /g'`)
> san@~ :> echo ${myarr[@]}
> 
> ```
> 
> foo bar baz

This led to an error "Syntax error: "(" unexpected" on my ubuntu,  
I executed it in a script started with:

```plaintext
 #!/bin/sh 

```

---------- Post updated at 07:27 PM ---------- Previous update was at 07:19 PM ----------

> [@gary\_w](#):
>
> ```plaintext
> $ cat x
> #!/bin/ksh
> 
> IFS="|"
> STRING="foo|bar|baz"
> 
> set -A array $STRING
> 
> print ${array[0]}
> print ${array[1]}
> print ${array[2]}
> 
> exit 0
> $ x
> foo
> bar
> baz
> $
> 
> ```

This led to an error "Illegal option -A" on my ubuntu,  
I execute it in a script started with:

```plaintext
 #!/bin/sh 

```

Maybe it is related to the mismatch of the syntax and the shell version on my machine.

Thanks a lot  
Roy987

---

<div class="post-metadata">

**Author:** ![gary\_w](https://community.unix.com/user_avatar/community.unix.com/gary_w/32/2088_2.png) [@gary\_w](https://community.unix.com/u/gary_w)\
**Post date:** [May 16, 2012, 9:26pm UTC](https://community.unix.com/t/split-string-into-array-in-shell/310292/5 "2012-05-16T21:26:30Z")

</div>

Yes I believe the Bourne shell (sh) does not support arrays. Are you able to run my complete example? Make sure to include the first line, it tells whatever your login shell is to use the korn shell to execute the script.

You may have better luck using bash (bourne again shell) heh on Linux.

---

<div class="post-metadata">

**Author:** ![Roy987](https://community.unix.com/letter_avatar/roy987/32/6_449c533f586939b6e5f246632a46421f.png) [@Roy987](https://community.unix.com/u/Roy987)\
**Post date:** [May 16, 2012, 9:58pm UTC](https://community.unix.com/t/split-string-into-array-in-shell/310292/6 "2012-05-16T21:58:29Z")

</div>

Thanks.  
Actually I will ship the code to an embedded device after writing it on ubuntu, on which it is required to use Bourne Shell instead of other options, then I will try other method to pass on data.
