Bash

Slice Bash Array

admin by @admin ADMIN
3d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Bash supports Python-like array slicing via ${arr[@]:offset:length}. Lets you take/skip elements without external tools.
Bash
Raw
arr=(a b c d e f g)

echo "${arr[@]:2}"        # c d e f g     — from index 2 to end
echo "${arr[@]:2:3}"      # c d e         — 3 elements starting at index 2
echo "${arr[@]:0:1}"      # a             — first element only
echo "${arr[@]: -2}"      # f g           — last 2 (note the space before -)
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.