> There is one more really cool thing to know about sourcing. You can use a single dot (.) as a shortcut for the source command.
That's backwards. Dot is the POSIX-standard command; source isn't. So it's more like, in some shells you can use the readable source command instead of the cryptic dot, at the cost of portability.
source does not go without a mention in POSIX. The specification says that if the command name in a command matches one of a certain set of identifiers, the results are unspecified. source is one of those identifiers.
This can be dangerous on untrusted files, because it's easy to forget that it will run arbitrary shell commands, rather than just setting variables to static values.
Yeah, this essentially the old "parse json with eval" (or more generally configuration file is code, but json vs eval was a ceaseless problem that was fairly regularly actually exploited rather than just being theoretically exploitable).
Fun fact: the reason json has as many constraints as it does is because rather than simply parsing json (a trivial thing) the json[2].js implementation used a regex to "validate" it before .. throwing it at eval. That necessarily required huge restrictions on what could be there - you can't use "undefined" in json because it's not actually a keyword, no comments (can't handle those with regex), etc all come from that :-/
I read that comments were forbidden in json because some json generators/parsers started putting data about how to interpret them in comments, fracturing the standard.
Loading json as javascript also led to seemingly odd patterns, like putting an infinite loop before the start of a json payload to prevent injected javascript stealing data.
The article started out really well but ended too soon.
Often you want a source of truth that is true for more than just your bash script.
It should have included a discussion about how sourcing config lets the config author execute code.
I also think you want to export these values. You source your config, maybe setup an environment and then execute a Python script that reads values from the environment.
Other things I was hoping for: a lot of modern tools come with config in json. Is there a reasonable way to read this into bash script?
Yeah the article is pretty terrible IMO. It seems to be pitched at folks who don't know any better, so just showing that you can use 'source' like a kind of config parser without pointing out that it totally just executes code is pretty reckless.
I thought it was going to be something more like "here's a way to read KEY=VALUE lines into an associative array" (which is pretty easy, and can lead to all sorts of fun discussions/learnings about arrays in bash, subshells, whitespace/$IFS, etc)
This made me curious about what macOS does. Using the finder, whether via menu command or drag and drop will rename the file automatically to
${filename} hh.mm.ss.${ext}
But mv does not. Interestingly, it doesn’t appear Apple has added their own cli tool for moving something to the trash in the same way that they provide `open` or `pbcopy` or `pbpaste`
You could execute inline AppleScript to tell the Finder to move the file to the trash, but that’s pretty roundabout
That's backwards. Dot is the POSIX-standard command; source isn't. So it's more like, in some shells you can use the readable source command instead of the cryptic dot, at the cost of portability.
source does not go without a mention in POSIX. The specification says that if the command name in a command matches one of a certain set of identifiers, the results are unspecified. source is one of those identifiers.