-3

I have an array declared in my script.

NAME[0]=Deepak
NAME[1]=Renuka
NAME[2]=Joe
NAME[3]=Alex
NAME[4]=Amir

echo "All Index: ${NAME[*]}" echo "All Index: ${NAME[@]}"

There are two ways to print whole array which is shown above. Can some please write the difference between those methods?

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
Tom
  • 5

1 Answers1

2
  • echo "All Index: ${NAME[*]}" equals to echo "All Index: ${NAME[0]} ${NAME[1]} ${NAME[2]} ${NAME[3]} ${NAME[4]}"
  • echo "All Index: ${NAME[@]}" equals to echo "All Index: ${NAME[0]}" "${NAME[1]}" "${NAME[2]}" "${NAME[3]}" "${NAME[4]}" if the first character of IFS variable is a space (default)

You can see the execution result in copy.sh.


The default value of IFS variable is $' \t\n'. ${array[*]} and $* output strings splited by the first character of IFS variable. It is also possible to change the character to split.

NAME[0]=Deepak
NAME[1]=Renuka
NAME[2]=Joe
NAME[3]=Alex
NAME[4]=Amir

IFS=: echo "All Index: ${NAME[*]}"

Output: All Index: Deepak:Renuka:Joe:Alex:Amir

IFS= echo "All Index: ${NAME[*]}"

Output: All Index: DeepakRenukaJoeAlexAmir

IFS=$', \t\n' echo "All Index: ${NAME[*]}"

Output: All Index: Deepak,Renuka,Joe,Alex,Amir

sakkke
  • 193