You are viewing a single comment's thread. Return to all comments →
countries=() while IFS= read -r line; do countries+=("$line") done
start=3 end=7
length=$(( end - start + 1 ))
slice=("${countries[@]:start:length}")
echo "${slice[@]}"
Seems like cookies are disabled on this browser, please enable them to open this website
Slice an Array
You are viewing a single comment's thread. Return to all comments →
!/bin/bash
Read all lines into an array
countries=() while IFS= read -r line; do countries+=("$line") done
Define start and end indices (0-based)
start=3 end=7
Calculate length of slice
length=$(( end - start + 1 ))
Slice the array and print
slice=("${countries[@]:start:length}")
echo "${slice[@]}"