We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Slice an Array
Slice an Array
Sort by
recency
|
140 Discussions
|
Please Login in order to post a comment
!/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[@]}"
head -8 | tail +4 | paste -s -d " "
my solution
Using mapfile: