I thought to post up a Q&A on this as I did not find anything similar. If it already exists, please mark this as a duplicate.
The following code, running under Bash shell, doesn't work (should return just f
, the last (-1-th) item in $@
):
$ set -- a b c d e f
$ echo ${@:-1}
a b c d e f
To get the last element of the list using list. pop(), the list. pop() method is used to access the last element of the list.
The size() method returns the number of elements in the ArrayList. The index values of the elements are 0 through (size()-1) , so you would use myArrayList. get(myArrayList. size()-1) to retrieve the last element.
The best way to get the last element of a list in Python is using the list[-1]. Python allows you to use negative indices, which count from the end of the list instead of the beginning. So list[-1] gets the last element, list[-2] gets the second to last.
The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. The last element of the Set can be deleted by by passing its iterator.
${parameter:-word}
is a type of parameter expansion:
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
So ${@:-1}
is interpreted as:
$@
is unset or null, substitute with 1
$@
as it isSince the latter is true, echo
prints $@
in all its glory
Put some space between :
and -
:
$ echo ${@: -1}
f
As an alternative syntax to the space suggested by Zaid, you can also use round brackets to get the functionality you want:
echo ${@:(-1)}
f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With