As a follow-up to my own post ... I'm embarrassed by Hugh and John's follow-ups. Hugh suggested sourcing the script in parentheses so as not to pollute your local environment (this works by running it in a subshell), and John suggested running the script by running it in a shell ("sh scriptfile" - this has a similar outcome to Hugh's suggestion). Both are good ideas. And I'm embarrassed because ... the truth is I always do the latter, ie. `bash scriptfile` which is NOT the same as "sourcing" the file (as I implied in my previous message). They're different things. Usually a small difference, occasionally a big one. My apologies for this small (but occasionally big) difference. On Mon, 20 Apr 2026 at 10:24, Giles Orr <gilesorr@gmail.com> wrote:
On Sat, 18 Apr 2026 at 16:46, D. Hugh Redelmeier via Talk <talk@lists.gtalug.org> wrote:
You don't accidentally source a script: it takes explicitly using "source" or "." as a command. You don't do that with any program, why should shell scripts be different?
The ONLY reason to use "source" is if you want the script to modify the current shell's environment. Dicey, but sometimes useful.
I can name one other circumstance where I've used `source` to run a script. A bit hacky, but useful. Sometimes partitions or external media are mounted with the execute bit set off. (This is a good security practice for unknown media, but annoying for "my" drives.) In that case, you can't directly run the script. You either have to copy it to another part of the filesystem or use `source`. It works - mostly.
-- Giles https://www.gilesorr.com/ gilesorr@gmail.com
-- Giles https://www.gilesorr.com/ gilesorr@gmail.com