Fwd: [kwlug-disc] Weird but handy shell tricks
Yesterday's meeting was a fun deep-dive into some `bash` quirks. Mostly on how to determine if a script was sourced or run as an executable. The reason being the use of `exit` statements as "gate clauses" to detect error conditions and abort instead of nesting the desired code inside if...elif...else...fi statements. In a sourced script, `exit` will close the user's terminal. We poured over a technique I found on StackOverflow years ago that checks for an error on calling `return` in an executed script (it's illegal there) and quits the program if the error does not occur. Requires masking error messages generated when script is executable, running `set +e`, restoring the status of `set -e`, and other nonsense. Hugh suggested that scripts should never be sourced unless specific conditions are desired. Not wrong, but the desire is to prevent users' terminal from closing on them, forcing the correct invocation. Lennart came up with a clever technique, and even improved upon it by embedding it in a function. I went with that method, then modified further to simply test for *$0 == -bash*, which is always true if the script was sourced. Thanks everyone that joined the meeting and especially Lennart for his contribution. Also, some clever shell tricks shared on the KWLUG mailing list recently, highly worth a quick read:
Not sure who could benefit from this, but here's a site with some useful shell tricks:
https://blog.hofstede.it/shell-tricks-that-actually-make-life-easier-and-sav...
Ron via Talk said on Thu, 16 Apr 2026 00:38:31 -0700
Not sure who could benefit from this, but here's a site with some useful shell tricks:
https://blog.hofstede.it/shell-tricks-that-actually-make-life-easier-and-sav...
Very, VERY NICE! I'm copying this to GoLUG. SteveT Steve Litt http://444domains.com
The reference to `tee` near the end reminds me of: echo 'specialFlag' | sudo tee /proc/foo/bar to minimize the amount of time you're sudo'ed. On Thu, 16 Apr 2026 at 23:31, Steve Litt via Talk <talk@lists.gtalug.org> wrote:
Ron via Talk said on Thu, 16 Apr 2026 00:38:31 -0700
Not sure who could benefit from this, but here's a site with some useful shell tricks:
https://blog.hofstede.it/shell-tricks-that-actually-make-life-easier-and-sav...
Very, VERY NICE!
I'm copying this to GoLUG.
SteveT
Steve Litt
http://444domains.com ------------------------------------ Description: GTALUG Talk Unsubscribe via Talk-unsubscribe@lists.gtalug.org Start a new thread: talk@lists.gtalug.org This message archived at https://lists.gtalug.org/archives/list/talk@lists.gtalug.org/message/AXTTG2J...
On Thu, Apr 16, 2026 at 12:38:31AM -0700, Ron via Talk wrote:
https://blog.hofstede.it/shell-tricks-that-actually-make-life-easier-and-sav...
I see it mentiens 'reset' for fixing a messed up terminal. In my experience sometimes that isn't enough. I have found that more effective is: cat <esc>c<ctrl-d><ctrl-d> ie you send: reset RIS Reset terminal to initial state ^[c then end the cat using end of file. I guess it could be written as ^[c^d^d for people that like that syntax. -- Len Sorensen
From: Ron via Talk <talk@lists.gtalug.org>
Yesterday's meeting was a fun deep-dive into some `bash` quirks.
Mostly on how to determine if a script was sourced or run as an executable.
Hugh suggested that scripts should never be sourced unless specific conditions are desired. Not wrong, but the desire is to prevent users' terminal from closing on them, forcing the correct invocation.
I stand by that :-) 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 did learn that BASH is even weirder that I remembered. I learned to use the Bourne Shell almost 50 years ago. It was a big improvement over the earlier UNIX shell. But it is a bit odd. BASH is a LOT more complicated and most of the features don't pay for their additional complexity. I do admit that some features are great. - readline! (But I used that kind of command editing in the earlier Korn Shell (from Unix Systems Group)). - there are plenty of hacky features to improve programming, but, even so, I don't think that BASH is a good language for programming. In some sense, the successor the the Bourne Shell is Tom Duff's RC shell for Plan 9. I haven't used it, but I helped Byron Rakitzis with his UNIX/Linux implementation. I just installed it from the Fedora repos. This version seems to be configured to use GNU readline. <https://utcc.utoronto.ca/~cks/space/blog/unix/RcHistoryHowItWorks> BASH is really really fat: $ size /usr/bin/bash /usr/bin/dash /usr/bin/rc text data bss dec hex filename 1426035 49208 47072 1522315 173a8b /usr/bin/bash 109024 5016 11408 125448 1ea08 /usr/bin/dash 90580 3368 6344 100292 187c4 /usr/bin/rc Even more telling is the size of the man pages for each: $ man bash | wc 7430 58219 444766 $ man dash | wc 1601 10177 78627 $ man rc | wc 1139 6458 46263
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
From: Giles Orr via Talk <talk@lists.gtalug.org>
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.
Right. Maybe use ( . scriptfile ) to avoid affecting your environment?
On Mon, 2026/04/20 02:50:52PM -0400, D. Hugh Redelmeier via Talk <talk@lists.gtalug.org> wrote: | > From: Giles Orr via Talk <talk@lists.gtalug.org> | | > 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. | | Right. Maybe use | ( . scriptfile ) | to avoid affecting your environment? Or "sh scriptfile" ... % cat hello #!/bin/sh echo hi there % chmod 755 hello % ./hello bash: ./hello: Permission denied % sh hello hi there
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
D. Hugh Redelmeier via Talk said on Sat, 18 Apr 2026 16:45:53 -0400
BASH is really really fat:
$ size /usr/bin/bash /usr/bin/dash /usr/bin/rc text data bss dec hex filename 1426035 49208 47072 1522315 173a8b /usr/bin/bash 109024 5016 11408 125448 1ea08 /usr/bin/dash 90580 3368 6344 100292 187c4 /usr/bin/rc
I use bash only as an interactive shell. For programming (scripting) I use ksh, which is almost completely identical to bash on a scripting basis. You can tell shellcheck to check a shellscript as a ksh file, and if your shellscript has a ksh shebang, this is automatic. [slitt@mydesk ~]$ size /usr/bin/bash /usr/bin/ksh /usr/bin/dash /usr/bin/rc text data bss dec hex filename 1462032 58629 47624 1568285 17ee1d /usr/bin/bash 272429 7039 34704 314172 4cb3c /usr/bin/ksh 113861 4848 11568 130277 1fce5 /usr/bin/dash size: '/usr/bin/rc': No such file [slitt@mydesk ~]$ Ksh isn't as svelte as dash, but it sure makes a nice shellscript language. And it's not as fat as bash. SteveT Steve Litt http://444domains.com
participants (7)
-
D. Hugh Redelmeier -
David Mason -
Giles Orr -
John Sellens -
Lennart Sorensen -
Ron -
Steve Litt