Subset a bash array

One can create a bash array using the following command

my_arr=('A' 'B' 'C')        

To just to get the last two elements, for example, one can use the following command

sub_arr=${my_arr[@]:1:2}
echo ${sub_arr[@]} # output to check        

The first number 1 after the array tells from which index (array index starts from 0) to start slicing, and the next number 2 tells how many elements from the start.

Hope this is helpful.

To view or add a comment, sign in

More articles by Zhenguo Zhang

Insights from the community

Explore topics