Discussion:
[Help-bash] Using slices with arrays
Nick Chambers
2017-10-04 20:02:23 UTC
Permalink
Hello all! I am using bash4.4 and have some questions about slices in bash. For my first example:

NickChambers-iMac:~ Nick$ str='this is a message'
NickChambers-iMac:~ Nick$ echo "${str:4:(-1)}"
is a messag
NickChambers-iMac:~ Nick$

This clearly allows a negative index, but if I try to do the same thing with an array, I get the following:

NickChambers-iMac:~ Nick$ colors=(purple blue orange yellow green)
NickChambers-iMac:~ Nick$ echo "${colors[@]:1:3}" # expected
blue orange yellow
NickChambers-iMac:~ Nick$ echo "${colors[@]:1:(-1)}"
-bash: (-1): substring expression < 0
NickChambers-iMac:~ Nick$

Is there a reason why this can be done on strings but not on arrays?
Also, incidentally, is there a better way to span from an index to the end of the string besides using something like the following:

NickChambers-iMac:~ Nick$ str='this is a message'
NickChambers-iMac:~ Nick$ strlen=${#str}
NickChambers-iMac:~ Nick$ echo "${str:4:strlen}"
is a message
NickChambers-iMac:~ Nick$
Greg Wooledge
2017-10-04 20:09:12 UTC
Permalink
Post by Nick Chambers
NickChambers-iMac:~ Nick$ str='this is a message'
NickChambers-iMac:~ Nick$ echo "${str:4:(-1)}"
is a messag
NickChambers-iMac:~ Nick$
This clearly allows a negative index,
But it's not necessary. You can simply use "${str:4}" to get the substring
from position 4 to the end.

wooledg:~$ str="this is a message"
wooledg:~$ echo "${str:4}"
is a message
Post by Nick Chambers
NickChambers-iMac:~ Nick$ colors=(purple blue orange yellow green)
-bash: (-1): substring expression < 0
Again, you just omit the length argument if you want to go all the way
to the end.

wooledg:~$ colors=(purple blue orange yellow green)
wooledg:~$ args "${colors[@]:1}"
4 args: <blue> <orange> <yellow> <green>
Just omit the length.

Only specify a length when you want (at most) that many elements.

wooledg:~$ args "${colors[@]:1:2}"
2 args: <blue> <orange>
wooledg:~$ args "${colors[@]:3:1}"
1 args: <yellow>
Nick Chambers
2017-10-04 20:10:37 UTC
Permalink
Post by Greg Wooledge
Post by Nick Chambers
Hello all! I am using bash4.4 and have some questions about slices in
NickChambers-iMac:~ Nick$ str='this is a message'
NickChambers-iMac:~ Nick$ echo "${str:4:(-1)}"
is a messag
NickChambers-iMac:~ Nick$
This clearly allows a negative index,
But it's not necessary. You can simply use "${str:4}" to get the substring
from position 4 to the end.
wooledg:~$ str="this is a message"
wooledg:~$ echo "${str:4}"
is a message
Post by Nick Chambers
NickChambers-iMac:~ Nick$ colors=(purple blue orange yellow green)
-bash: (-1): substring expression < 0
Again, you just omit the length argument if you want to go all the way
to the end.
wooledg:~$ colors=(purple blue orange yellow green)
4 args: <blue> <orange> <yellow> <green>
Post by Nick Chambers
Also, incidentally, is there a better way to span from an index to the
Just omit the length.
Only specify a length when you want (at most) that many elements.
2 args: <blue> <orange>
1 args: <yellow>
Perfect. Thank you!

Loading...