Skip to content

Bug: Substring in Bash :: Slicing - Examples incorrect #153

Closed
@Skinner927

Description

@Skinner927

Describe the bug

The section Substring in Bash :: Slicing, nested under Bash Arrays is not operating on an arrays at all. It should not be nested under the Arrays section.

The letters array in the examples is presumed to be an array with multiple elements. Instead, because there are no spaces, the letters array is an array with a single element.

#!/bin/bash
letters=( "A""B""C""D""E" )
echo ${letters[@]}

In later examples the array is "sliced":

#!/bin/bash
letters=( "A""B""C""D""E" )
b=${letters:0:2}
echo "${b}"
# AB

This command will print array from starting index 0 to 2 where 2 is
exclusive.

The text says this is operating on the arrays indexes, but it's actually slicing a string. $letters without an index ([x]) will return index 0 by default, thus the "ABCDE" string.

The examples work almost by accident and are a poor illustration of how to access ranges.

To Reproduce

If letters was actually an array (replaced with real here) the examples would not work.

letters=( "A""B""C""D""E" )
b=${letters:0:2}
echo "${b}"
# AB

real=( "A" "B" "C" "D" "E" )
echo "${real:0:2}"
# A

Expected behavior

I believe the slicing section should be moved out from under the Arrays heading and into a Strings section, or anywhere else.

The examples should change the definition of letters to letters="ABCDE" and no other changes would be needed since these are all string operations.

Screenshots

image

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions