# Question about sorting -- how to pass an array to a function

**URL:** <https://community.unix.com/t/question-about-sorting-how-to-pass-an-array-to-a-function/331167>\
**Category:** Shell Programming and Scripting\
**Created:** [June 28, 2013, 1:55pm UTC](https://community.unix.com/t/question-about-sorting-how-to-pass-an-array-to-a-function/331167 "2013-06-28T13:55:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![franksunnn](https://community.unix.com/letter_avatar/franksunnn/32/5_5575768a8748004e209b776fc1b2916d.png) [@franksunnn](https://community.unix.com/u/franksunnn)\
**Post date:** [June 28, 2013, 1:55pm UTC](https://community.unix.com/t/question-about-sorting-how-to-pass-an-array-to-a-function/331167/1 "2013-06-28T13:55:34Z")

</div>

Hi, guys

I just wanted to sort the elements of an array ascendingly.  
I know the following code does work well:

```nohighlight
array=(13 435 8 23 100)
for i in {0..4}
do
	j=$((i+1))
	while [[$j -le 4]]
	do
		if [[${array[$i]} -le ${array[$j]} ]]
		then :
		else
			min=${array[$j]}
			${array[$j]}=${array[$i]}
			${array[$i]}=$min
		fi
		((j++))
	done
done

```

However, I wanted to write a function named "sort" and pass the array as argument to the function.  
I know the following code doesn't work:

```nohighlight
array=(13 435 8 23 100)
sort()
{
for i in {0..4}
do
	j=$((i+1))
	while [[$j -le 4]]
	do
		if [[${$1[$i]} -le ${$1[$j]} ]]
		then :
		else
			min=${$1[$j]}
			${$1[$j]}=${$1[$i]}
			${$1[$i]}=$min
		fi
	done
done
}
sort array

```

And I know it'll be better by using the command "eval" to pass the array to the function like this:

```nohighlight
eval first_num=\${$1[$i]} 

```

but I can't put it to the correct place in the function.  
So please give me some ideas~~ Thx

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/5_5575768a8748004e209b776fc1b2916d.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [June 28, 2013, 3:15pm UTC](https://community.unix.com/t/question-about-sorting-how-to-pass-an-array-to-a-function/331167/2 "2013-06-28T15:15:06Z")

</div>

Using eval means you can pass names as string parameters, put that string into a command string, and eval theat command string to get the shell to re-parse it as script. For instance:

```nohighlight
pick_an_arg(){
  arg_no=$1
  shift
  eval 'echo "$'"$arg_no"'"'
 }

```

lets you print the fourth arg using arguments: 3 ? ? fourth\_arg ? ? ? ?

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [June 28, 2013, 3:36pm UTC](https://community.unix.com/t/question-about-sorting-how-to-pass-an-array-to-a-function/331167/3 "2013-06-28T15:36:49Z")

</div>

Be very very careful using eval, however. It will parse any valid shell syntax it finds -- including things you might not have intended, such as string contents. Someone putting `rm -Rf ~/` into your array could be very bad.

---

<div class="post-metadata">

**Author:** ![franksunnn](https://community.unix.com/letter_avatar/franksunnn/32/5_5575768a8748004e209b776fc1b2916d.png) [@franksunnn](https://community.unix.com/u/franksunnn)\
**Post date:** [June 28, 2013, 3:42pm UTC](https://community.unix.com/t/question-about-sorting-how-to-pass-an-array-to-a-function/331167/4 "2013-06-28T15:42:00Z")

</div>

> [@corona688](#):
>
> Be very very careful using eval, however. It will parse any valid shell syntax it finds -- including things you might not have intended, such as string contents. Someone putting `rm -Rf ~/` into your array could be very bad.

So how about my question? How to pass the array as argument to the function?  
:)🙂

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [June 28, 2013, 4:02pm UTC](https://community.unix.com/t/question-about-sorting-how-to-pass-an-array-to-a-function/331167/5 "2013-06-28T16:02:49Z")

</div>

You've been given the answer. Pass the array name into the function, assemble the statement you want as text in a string, then use eval to parse that text as a shell statement (since shell will not do that kind of double-think unless you ask).

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/5_5575768a8748004e209b776fc1b2916d.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [June 28, 2013, 4:05pm UTC](https://community.unix.com/t/question-about-sorting-how-to-pass-an-array-to-a-function/331167/6 "2013-06-28T16:05:58Z")

</div>

Quoting of anything going into eval is helpful, and if the input is human, maybe a check for meta-characters. It is very like preventing a SQL Injection attack (which were allowed by sloppy SQL tool choices).
