Timeline for Getting the last argument passed to a shell script
Current License: CC BY-SA 4.0
7 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 8, 2020 at 11:06 | comment | added | AgileZebra | Thanks dkasak. Updated to reflect your simplification. | |
Jun 8, 2020 at 11:05 | history | edited | AgileZebra | CC BY-SA 4.0 |
deleted 7 characters in body
|
Jun 6, 2020 at 7:03 | history | edited | oguz ismail | CC BY-SA 4.0 |
added 28 characters in body
|
Nov 4, 2016 at 20:02 | comment | added | dkasak |
AgileZebra's answer supplies a way of getting all but the last arguments so I wouldn't say Steven's answer supersedes it. However, there seems to be no reason to use $((...)) to subtract the 1, you can simply use ${@:1:$# - 1} .
|
|
Nov 26, 2015 at 8:05 | comment | added | oHo |
The Steven Penny's answer is a bit nicer: use ${@: -1} for last and ${@: -2:1} for second last (and so on...). Example: bash -c 'echo ${@: -1}' prog 0 1 2 3 4 5 6 prints 6 . To stay with this current AgileZebra's approach, use ${@:$#-1:1} to get the second last. Example: bash -c 'echo ${@:$#-1:1}' prog 0 1 2 3 4 5 6 prints 5 . (and ${@:$#-2:1} to get the third last and so on...)
|
|
Feb 24, 2015 at 11:51 | history | edited | AgileZebra | CC BY-SA 3.0 |
changed per @starfry. Quite right, $# is all that'
|
Mar 31, 2011 at 6:30 | history | answered | AgileZebra | CC BY-SA 2.5 |