Skip to main content
14 events
when toggle format what by license comment
Jan 13 at 18:33 comment added midnite To my opinion this is the best and neat answer. But why you guys do not quote shift "${1}"?
Mar 7, 2023 at 18:18 comment added Adam Katz @polynomial_donut – last() { shift $(($# - 1)) 2>/dev/null && printf %s "$1"; } will silently exit (with error code 1) if not given an argument
Feb 5, 2023 at 19:06 comment added polynomial_donut I'm getting "0" returned if there are no arguments passed, that should be considered
Dec 2, 2020 at 13:20 comment added Léa Gris @MichałŠrajer i Think if you are the guy tasked to write scripts for that 40 years old legacy SCO Unix, you are likely to know your way with the man pages and printed manual because browsing stackoverflow with Mozaic is going to be quite challenging on its own.
Dec 2, 2020 at 12:37 comment added Michał Šrajer @LéaGris I believe you wound need to use expr instead of $(()) to make it work on all unix systems.
Nov 19, 2020 at 15:03 comment added Léa Gris Alternatively: last() { shift $(($# - 1));printf %s "$1";}
Jan 10, 2018 at 18:20 comment added joki @MichałŠrajer consider the case where "$1" is "-n" or "--", so for example ntharg 1 -n or ntharg 1 -- may yield different results on various systems. The code you have now is safe!
Jan 9, 2018 at 14:11 comment added Michał Šrajer thanks @joki I worked with many different unix systems and I wouldn't trust echo -n either, however I am not aware on any posix system where echo "$1" would fail. Anyhow, printf is indeed more predictable - updated.
Jan 9, 2018 at 14:08 history edited Michał Šrajer CC BY-SA 3.0
updated as suggested in comment
Jan 4, 2018 at 9:37 comment added joki This is a great idea, but I have a couple of suggestions: Firstly quoting should be added both around "$1" and "$#" (see this great answer unix.stackexchange.com/a/171347). Secondly, echo is sadly non-portable (particularly for -n), so printf '%s\n' "$1" should be used instead.
Oct 7, 2016 at 9:45 comment added Ján Lalinský Great answer - short, portable, safe. Thanks!
Feb 20, 2013 at 8:46 history edited Michał Šrajer CC BY-SA 3.0
added 6 characters in body
Feb 1, 2013 at 16:47 history edited Michał Šrajer CC BY-SA 3.0
extended description
Feb 1, 2013 at 15:47 history answered Michał Šrajer CC BY-SA 3.0