Skip to main content

Timeline for choosing between $0 and BASH_SOURCE

Current License: CC BY-SA 4.0

25 events
when toggle format what by license comment
May 24 at 23:23 comment added mklement0 I appreciate the nice feedback, @hek2mgl. The reason that $BASH_SOURCE is mentioned nonetheless is that not everyone uses shellcheck.net (notwithstanding its usefulness), and those that don't won't care about the latter's warnings. The middle section is for those who want to dig deeper and understand the trade-offs.
May 24 at 23:02 comment added hek2mgl Didn't you know that people will only read the first sentence? :) ... Joke aside, and to be clear: I think this answer is super helpful and well written, no doubt. I just think the more simply ... part in the first sentence is unnecessary. If you take shellcheck into account, it's not simpler and therefore I wouldn't mention it. Please take this only as a small side-note. Great answer!
May 24 at 11:25 history edited mklement0 CC BY-SA 4.0
deleted 2 characters in body
May 24 at 11:25 comment added mklement0 @hek2mgl, the entire middle section of this answer is dedicated to this discussion. ${BASH_SOURCE[0]} is enough to silence the warning.
May 24 at 11:06 comment added hek2mgl Hi. shellcheck gives a warning when using ${BASH_SOURCE} without index: "C2128 (warning): Expanding an array without an index only gives the first element.". The warning could be silenced, but this would probably speak against the argument of simplicity as it requires more keystrokes to silence the warning than simply using ${BASH_SOURCE[0]}. Having this, and since shellcheck is a good thing which people should use, I would not recommend to use ${BASH_SOURCE}
Jan 23, 2023 at 22:35 comment added mklement0 Thanks, @MarkReed - I've updated the answer to fix the incorrect statement, but I didn't go into as much depth as your comment does, given that the original comment was just an aside.
Jan 23, 2023 at 22:16 history edited mklement0 CC BY-SA 4.0
added 54 characters in body
Jan 23, 2023 at 22:03 comment added Mark Reed "(By contrast, zsh, ever the renegade, indeed does return the first element, irrespective of its index)." Actually, in zsh, an unsubscripted array parameter expansion becomes the whole array, not just the first item. Without shwordsplit set,$ary ~= "${ary[@]}", "$ary" ~= "${ary[*]}", and $=ary ~= ${ary[*]} or ${ary[@]} without the quotes.
Apr 23, 2021 at 22:49 history edited mklement0 CC BY-SA 4.0
added 1 character in body
Apr 23, 2021 at 22:24 comment added mklement0 @CharlieParker, please see the new bottom section I've added to the answer.
Apr 23, 2021 at 22:24 history edited mklement0 CC BY-SA 4.0
added 1049 characters in body
Apr 10, 2021 at 1:20 history edited mklement0 CC BY-SA 4.0
added 2 characters in body
Aug 2, 2019 at 2:00 history edited mklement0 CC BY-SA 4.0
added 115 characters in body
May 15, 2019 at 4:34 comment added mklement0 @AlexanderMills Yes, if you're using Bash, $BASH_SOURCE is the better choice.
May 15, 2019 at 3:03 comment added Alexander Mills so $BASH_SOURCE is more generic and works in more circumstances?
S Nov 8, 2018 at 17:21 history suggested dungdm93 CC BY-SA 4.0
add more example to more clear
Nov 8, 2018 at 16:40 review Suggested edits
S Nov 8, 2018 at 17:21
Nov 2, 2017 at 9:52 history edited mklement0 CC BY-SA 3.0
added 112 characters in body
Nov 2, 2017 at 9:40 history edited mklement0 CC BY-SA 3.0
added 1166 characters in body
May 23, 2017 at 12:02 history edited URL Rewriter Bot
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Mar 2, 2017 at 13:14 history edited mklement0 CC BY-SA 3.0
deleted 6 characters in body
Jan 26, 2016 at 3:33 vote accept H2ONaCl
Jan 26, 2016 at 3:26 history edited mklement0 CC BY-SA 3.0
added 224 characters in body
Jan 26, 2016 at 3:20 history edited mklement0 CC BY-SA 3.0
added 224 characters in body
Jan 26, 2016 at 3:15 history answered mklement0 CC BY-SA 3.0