← Back to Bash Course | Chapter 6: Arrays | Lesson 5 of 10

Array Slicing

Slicing lets you pull out just a portion of an array (or string) by specifying a starting position and how many elements to take, without writing a manual loop.

Basic Slicing with Offset and Length

${arr[@]:offset:length} extracts a sub-list of elements starting at index offset, taking up to length elements from there. This avoids manually looping and building a new array by hand.

Example: Basic Slicing with Offset and Length

bash
#!/bin/bash
letters=(a b c d e f)
slice=("${letters[@]:1:3}")
echo "Slice: ${slice[@]}"

Omitting the Length

Leaving out the length argument, ${arr[@]:offset}, takes every element from offset through the end of the array. This is a convenient way to say 'everything after this point'.

Example: Omitting the Length

bash
#!/bin/bash
numbers=(10 20 30 40 50)
rest=("${numbers[@]:2}")
echo "From index 2 onward: ${rest[@]}"

Negative Offsets Count From the End

A negative offset counts backward from the end of the array, but it must be written with a space (or wrapped in parentheses) before the minus sign, like ${arr[@]: -2}, otherwise Bash parses it as the ${var:-default} expansion instead of a slice.

Warning: ${items[@]:-2} (no space) means something completely different -- it's the default-value expansion, not a slice.

Example: Negative Offsets Count From the End

bash
#!/bin/bash
items=(one two three four five)
last_two=("${items[@]: -2}")
echo "Last two items: ${last_two[@]}"

Slicing Strings the Same Way

The identical :offset:length syntax also works directly on plain string variables, extracting a substring by character position instead of array element position. This consistency makes slicing syntax easy to remember across both arrays and strings.

Example: Slicing Strings the Same Way

bash
#!/bin/bash
text="Hello, World!"
echo "Substring: ${text:7:5}"
Common Mistakes
  1. Assuming negative offsets count from the start like a normal index; a negative offset means 'count from the end', and it needs a space or parentheses before the minus sign (${arr[@]: -1}) so Bash doesn't confuse it with the :- default-value operator.
  2. Miscounting the length argument as an end index rather than a count; ${arr[@]:2:3} takes 3 elements starting at index 2, it does not mean 'up to index 3'.
  3. Forgetting slicing syntax applies to both arrays (${arr[@]:offset:length}) and strings (${str:offset:length}) with the same rules, but they operate on elements vs. characters respectively.
Chapter Summary
  • Syntax: ${arr[@]:offset:length} -- start at index offset, take up to length elements.
  • Omitting length takes everything from offset to the end of the array.
  • A negative offset counts from the end of the array, but needs a space before the - (${arr[@]: -2}) to avoid being parsed as the :- default-value expansion.
  • The same :offset:length slicing syntax works on plain strings, operating on characters instead of array elements.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.