Arrays in Shell Scripting Tutorial for Beginners

ARRAYS :-



korn and bash support one-dimensional arrays when the first element has the index 0 . Here's how you set and evaluate the value of the third element of the array prompt



prompt[2]="Enter your name"

echo ${prompt[2]}



Note that evaluation is done with { }, and prompt[2] is treated just like a variable. It, however, doesn't conflict with a variable prompt that you may also define in the same shell



set -A month_arr 0 1 2 3 4 5 6 7 8 9 10       korn only



month_arr=(0 1 2 3 4 5 6 7 8 9 )              bash only



array stores the number of days available in each of the 12 months



echo ${month_arr[6]}



using @ or * as subscript, you can display all elements of the array as well as the number of elements



echo ${month_arr[@]}



echo ${#month_arr[@]}     - length of array



No comments:

Post a Comment