# convert variable content to array

**URL:** <https://community.unix.com/t/convert-variable-content-to-array/207272>\
**Category:** Shell Programming and Scripting\
**Created:** [August 3, 2008, 1:20pm UTC](https://community.unix.com/t/convert-variable-content-to-array/207272 "2008-08-03T13:20:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alalush](https://community.unix.com/letter_avatar/alalush/32/5_5575768a8748004e209b776fc1b2916d.png) [@Alalush](https://community.unix.com/u/Alalush)\
**Post date:** [August 3, 2008, 1:20pm UTC](https://community.unix.com/t/convert-variable-content-to-array/207272/1 "2008-08-03T13:20:20Z")

</div>

Hi All,

I have a variable in a shell script which holds let say n paarmeteres with space separate them :

$var = par1 par2 par3 par4 parn;  
so if I print this variable this is what I'll see:  
par1 par2 par3 par4 parn

I need to insert each parameter to an array , so I can go over on each of this parameters with a loop command.  
Actually it doesn't have to be in an array , I just need to find away to go over on each parameter in this variable and preform some operation on it.

Any suggestions ?

Thanks

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/5_5575768a8748004e209b776fc1b2916d.png) [@era](https://community.unix.com/u/era)\
**Post date:** [August 3, 2008, 1:27pm UTC](https://community.unix.com/t/convert-variable-content-to-array/207272/2 "2008-08-03T13:27:16Z")

</div>

If there are no other special characters in the strings, simply loop over them.

```nohighlight
for f in $var; do
  echo I see "$f"
done

```

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [August 3, 2008, 1:28pm UTC](https://community.unix.com/t/convert-variable-content-to-array/207272/3 "2008-08-03T13:28:23Z")

</div>

A solution without using array :

```nohighlight
var='par1 par2 par3 par4'
for param in $var
do
   echo "Param=$param"
done

```

Output:

```nohighlight
Param=par1
Param=par2
Param=par3
Param=par4

```

Now with an array (under bash) :

```nohighlight
var='par1 par2 par3 par4'
declare -a array=( $var )
for (( i=0; i<${#array[*]}; i++ ))
do
   echo "Param=${array}"
done

```

Jean-Pierre.

---

<div class="post-metadata">

**Author:** ![summer\_cherry](https://community.unix.com/letter_avatar/summer_cherry/32/5_5575768a8748004e209b776fc1b2916d.png) [@summer\_cherry](https://community.unix.com/u/summer_cherry)\
**Post date:** [August 3, 2008, 10:39pm UTC](https://community.unix.com/t/convert-variable-content-to-array/207272/4 "2008-08-03T22:39:39Z")

</div>

use perl

```nohighlight
@arr=split(" ",$var);

```
