Timeline for Getting the last argument passed to a shell script
Current License: CC BY-SA 2.5
10 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 23, 2022 at 7:48 | comment | added | Johannes | You will get a shellcheck warning here, but it's a shellcheck bug, so you can disable the warning here. | |
Dec 12, 2021 at 10:28 | comment | added | Jani Uusitalo | Note that when called without arguments, this yields the value of $0, which may not be what's intended. | |
May 21, 2021 at 16:07 | comment | added | TrueY | Mind the space between ':' and '-'! :) I missed that for the 1st time... | |
Apr 5, 2021 at 19:50 | comment | added | GregD | Works for Bourne shell also, at least for me | |
Mar 18, 2019 at 17:38 | comment | added | Dennis Williamson |
@Mr.Llama: Another place to avoid $# is when iterating over arrays since, in Bash, arrays are sparse and while $# will show the number of elements in the array it's not necessarily pointing to the last element (or element+1). In other words, one shouldn't do for ((i = 0; i++; i < $#)); do something "${array[$i]}"; done and instead do for element in "${array[@]}"; do something "$element"; done or iterate over the indices: for index in "${!array[@]}"; do something "$index" "${array[$index]}"; done if you need to do something with the values of the indices.
|
|
Mar 18, 2019 at 17:26 | comment | added | Mr. Llama |
Note: This answer works for all Bash arrays, unlike ${@:$#} which only works on $@ . If you were to copy $@ to a new array with arr=("$@") , ${arr[@]:$#} would be undefined. This is because $@ has a 0th element that isn't included in "$@" expansions.
|
|
Feb 22, 2019 at 13:34 | review | Suggested edits | |||
Feb 22, 2019 at 13:50 | |||||
Mar 23, 2018 at 18:25 | comment | added | Steven Lu | I've been using this and it breaks in MSYS2 bash in windows only. Bizarre. | |
May 28, 2015 at 3:34 | comment | added | foo | For those (like me) wondering why is the space needed, man bash has this to say about it: > Note that a negative offset must be separated from the colon by at least one space to avoid being confused with the :- expansion. | |
Dec 6, 2009 at 0:26 | history | answered | Dennis Williamson | CC BY-SA 2.5 |