LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   what will be the echo output in this scenario (https://www.linuxquestions.org/questions/linux-newbie-8/what-will-be-the-echo-output-in-this-scenario-912999/)

snaveen58 11-11-2011 07:30 AM

what will be the echo output in this scenario
 
cmd ()
{
xyz=(abc def ghi jkl mno)
echo ${xyz[$2]}
}

I am sending some numbers from a function
cmd 12 59 30 25

I am bit confused what output did the echo would throw? I am new to shell scripting. I am sorry if this question is not valid.

trappa01 11-11-2011 08:48 AM

The echo won't show anything!
xyz will be an array where xyz[0]='abc' , xyz[1]='def', xyz[2]='ghi' .....etc

by calling the function with arguements 12, 59, 30 and 25, you are saying that $1=12, $2=59, $3=30 and $4=25

You then try to echo the 60th entry in the xyz array ( or xyz[59] ) which it doesn't have so there is nothing to display.

if you called the cmd function with cmd 2 1 3 0 it would have shown xyz[1] or "def"

I hope this is clear.

TB0ne 11-11-2011 09:34 AM

Quote:

Originally Posted by snaveen58 (Post 4521488)
Code:

cmd ()
{
xyz=(abc def ghi jkl mno)
echo ${xyz[$2]}
}

I am sending some numbers from a function
cmd 12 59 30 25

I am bit confused what output did the echo would throw? I am new to shell scripting. I am sorry if this question is not valid.

Why don't you just run the script and see what it does?

grail 11-11-2011 10:54 AM

Also you can put set -x at the top of your code to have the shell show you what it is doing.


All times are GMT -5. The time now is 08:55 AM.